Sharing variables between different .tpl.php templates in drupal - templates

I understand that to share variables between templates you have to use hook preprocess.
I think I've got the hook preprocess bit ok.
I've got this in my theme's template php file.
function rootcandy_preprocess_views_view_fields__default(&$vars) {
$vars ['brian'] = 'hello from brian';
In views-view-fields--default.tpl.php I've got this:
print $brian ;
So it prints out "hello from brian". Hooray.
However I want to print out the header of my View - which is available in views-view.tpl.php but not in views-view-fields.
This code below looks promising but I don't understand why it doesn't result in my header being printed out in views-view-fields--default.tpl.php
function rootcandy_preprocess_views_view_fields__default(&$vars)
{$vars['mytitle'] = $vars['view']->display[$vars['view']->current_display]->header['area'];
}
I have of course got print $mytitle ; in views-view-fields--default.tpl.php
Any ideas?
Thanks.

It is a matter of timing, when views-view-fields is parsing, views-view has been parsed and you can't get his variables - They are not 'existing' anymore.
Use a function to generate the value and refer to that function in views-view and in views-view-fields, or create a module to produce this value and use it freely in any template.

Related

Drupal preprocess_page variable passed to commerce-product-title.tpl.php does not load

I pass a variable in preprocess_page to the template files, so I think.
$vars['merk']=$merk;
It works fine when I call the variable in page.tpl.php . But when I call that variable in commerce-product-title.tpl.php it does not load, it returns null. I cannot find how to pass that variable to this tpl.php file.
Presuming that template is only used when displaying a product, which is a different entity to a page.
Try altering the vars in the product hook instead:
function hook_preprocess_commerce_product(&$vars) {
$vars['merk']=$merk;
}

Lua: Redirect extern function definitions to a specified table

I have one file "example.lua":
local function manipulate(something)
return string.rep(something, 3) -- repeats the given string
end
function apiFunction(somethingelse)
return manipulate(somethingelse)
end
and another files (main.lua) task is to "load"/"do" it:
loadAPI("example.lua", "externAPI") --< the part i need help with
externAPI.apiFunction("Test") --> should return TestTestTest
the thing that should happen is, that example.lua gets executed just like
dofile("example.lua")
but everything globally "defined" within example.lua (in this case the apiFunction) moves to the new generated global "externAPI" table and the rest (ie. manipulate) is hidden and only available from inside the example.lua file.
I've seen this bahaviour before in the minecraft mod "ComputerCraft" in which there is a function called "os.loadAPI("/somepath/sha-2") and it would define the definitions in the sha-2-chunk in the due to the name specified "sha-2"-table.
I've been searching for this sort of scoping/redirecting stuff for a while but there are no solutions putting the stuff into the new table.
I've been thinking of parsing the _G table after new indexes and move those to the new table but I'm sure there are some lua-magicians out here that know a much cleaner, better working solution to this.
All this is in one C lua_state* , so if there are any solutions adding this loadAPI function in C/C++ and just registrating it at the state this would be fine, too.
I've also looked at "require", but didn't seem to understand whether it does what I need.
Using Lua 5.2.3
Hope i didn't forget anything.
Thanks in advance :)
~InDieTasten
Try this:
function loadAPI(f,g)
_G[g]=setmetatable({},{__index=_G})
loadfile(f,"bt",_G[g])()
end
loadAPI("example.lua", "externAPI")
print(externAPI.apiFunction("Test"))

How to access predefined variables within cfscript?

The following code works:
<cfoutput>#$.currentURL()#</cfoutput>
However, within a function, "$" is not available.
<cfscript>
function myTest() {
return $.currentURL();
}
</cfscript>
Does anyone know what actually is the equivalent of $ within a function?
Likewise, #pluginConfig.getDirectory()# works when used directly in cfoutput. However, within a cfscript function, it reports "unknown variable pluginConfig."
Thank you for advance for guiding me in the right direction.
When writing code outside the Mura Event Scope (like you do with that function), you have to obtain an instance of the Mura Scope ($) yourself. This can be done using the following code:
$ = application.serviceFactory.getBean('$');
Next you'll have to initialise the instance using an event object, a struct with value pairs or a 'siteID':
$.init(event);
$.init(myStruct);
$.init(siteID);
The same counts for the pluginConfig, this you can abtain via the Mura Scope. You'll have to pass the pluginID, moduleID, name or package of the plugin:
$.getPlugin(pluginID);
$.getPlugin(moduleID);
$.getPlugin(name);
$.getPlugin(package);
An other option you have is to pass the Mura Scope and the pluginConfig as arguments to the function. When writing a small plugin, this might be the easier way. But when writting medium or large plugins, it will get a bit messy when you're passing along these objects all the time.
The $ is used as a special framework variable in some CF frameworks (like Mura). You will need to figure out the framework context (if any) your code is executing in

django dump out a variable

In Django can I dump out a variable to see what is in it within a view (not using command line)?
for example:
device = mobile(request)
print device
abort
or
device = mobile(request)
return HttpResponse(device)
Writing to a file is usually a fool-proof way to "dump" data as a debug method when you are working with a hooked framework that doesn't otherwise lend itself well to direct debugging.
e.g.
device = mobile(request)
with open('path/to/debug_out.txt', 'w') as outfile:
outfile.write(device)
abort
for convenience, you could put this in a predefined function in some debug helper module. Alternatively, you could use sys.excepthook to automatically write all exceptions to this file, before forwarding/re-raising them.
There is a similar question:
Django debug display all variables of a page
You can type
assert False, locals()
in your view to see all your view variables. Or use the {%debug%} template tag inside your template which will do the same.

Coldfusion: conflicting cfdump statements

I have a query l.q. l is var scoped:
var l = {};
If I dump l:
writeDump(var:l, abort:true);
I get
You can see that q (l.q) shows [undefined struct element]. However if I do:
writeDump(var:isQuery(l.q), abort:true);
I get yes as the output? Dumping l.q.recordCount returns 1.
If I dump l.q I get an error: Error Type java.lang.ArrayIndexOutOfBoundsException : [N/A].
This is causing me quite a headache as on my website I get variables randomly disappearing like this all the time.
Any ideas of what could be going on here? CF9.0.1 Ent running on Windows Server 2005.
It looks like whatever is populating your l.q variable is not working correctly. How is l.q being populated?
If you're using l = {} as a "local" scope replacement - don't ColdFusion 9 already has a local scope inside of functions and you can dump it.
Are these dumps in the same place? Remember that if you execute a query that does not return anything (such as an update or delete) but use the name parameter, that variable will be set to undefined. It sounds like this could be happening here.