in template.php many times they used classes_array..am not getting the meaning and why they using,..what is the purpose of classes_array and when we have to use that in drupal7 .tpl.php
example code:
if(in_array('administrator',array_values($variables['user']->roles)))
{
$variables['classes_array'][]="debug";
}
$variables['classes_array'] is used in preprocess functions. It adds classes to be used in rendering the element to be processed. In your example, a class named "debug" will be added to the html container of the rendered element: if the actual code is
function <YOUR THEME>_preprocess_html(&$variables) {
if (in_array('administrator',array_values($variables['user']->roles))) {
$variables['classes_array'][]="debug";
}
}
your theme will output a body tag like
<body class='debug [...OTHER CLASSES...]'>
for users with administrator role.
You can also add classes to nodes, or other kind of elements for which a preprocess hook is available. E.g. you could write a node preprocess function:
function <YOUR THEME>_preprocess_node($variables) {
$classes_array[] = 'my-class';
}
if you wanted to add 'my-class' to every node of your site.
In general, you will not find $classes_array among the defined variables in tpl.php files. Your theme will, most of the times, implode them in a $classes variable. It must be noted, however, that a kind of confusion arised over time, so different themes may use $classes_array, $attribute_array, $classes, $attributes['class'] and so on for the same purpose, so you should check your theme's documentation to find out what suits your case.
Related
I have a php function in a drupal module. This function outputs some random text. I want to attach this module to a Drupal article so that each time someone creates an article, random text will appear in it. How can I do this?
The solution here is to use a Drupal hook function to modify the content of the node.
Assuming your module is called "my_module":, you'd add another function to your my_module.module file as follows:
function my_module_node_view(&$node, $view_mode, $langcode) {
// We want to make sure this only applies to nodes of the content type "article"
if ($node->type == "article") {
// Append the output of your function to the body; this could easily be added to any other field as well
$node->body['und'][0]['value'] = $node->body['und'][0]['value'] . my_module_random_text_function();
}
}
Note: the $node object is automatically passed to this hook function by reference, so you don't need to worry about returning anything from the function.
If you wanted to apply this at the theme layer, you could use the theme_preprocess_node hook in your theme's template.php file, but your original question suggested that you'd already gone down the plugin route.
in smarty Smarty_Compiler.class.php performed some operation between two tags like {if}{/if}
if i want to get text within the new tag then how to proceedi tried inside
function _compile_tag($template_tag)
{
....
switch ($tag_command) {
-----
case 'newtag':
break;
case '/newtag':
break;
}
How can i get the content of tpl within the new tag
You should create a Smarty plugin. You can read documentations here (about extending Smarty) and here (more specific, about create block functions plugins).
Basically, you have to create your smarty_make_pdf() PHP function (see parameters in the second link I gave you), place it in a file called block.make_pdf.php (see here) and tell Smarty to search for plugins in the folder you created that file using $smarty->addPluginsDir() (see here).
PS: I'm supposing you are using Smarty 3.
You really shouldn't be editing the core Smarty code to achieve this.
Look into registerPlugin() if you're using Smarty 3 (or register_block() if you're on Smarty 2).
These methods will allow you to create your own Smarty tags and write handler functions that implement them.
the site I'm currently building in Drupal is split into two very different areas, both structured quite differently. My solution is to build two different page.tpl pages. Does anyone know what the easiest method is to implement this in Drupal 7. Is it a naming convention process like the node.tpl page or will I need to insert a preprocess function in the template.php page.
Cheers for any help!
You could also attach a preprocess function in your template.php file. Something like the following:
function yourthemename_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
// If the node type is "blog_madness" the template suggestion will be "page--blog-madness.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
}
}
Have a look at this page: http://drupal.org/node/1089656
It explains the naming schema for your template files and how to extend the template suggestions.
In your case, you'll have to implement the theme_get_suggestions() function. In this function you can check the path arguments to determine which kind of page you need to display and add the appropriate suggestions.
I don't get it . If you want to use two different template files then just create two different content types and you can by pass all this coding.
let me know if you need further help.
vishal
You can use the Context module.
Within a given context, you can add the "Template suggestions" reaction. This will allow you to specify additional template files that will override the defaults. See the issue in the Context queue, Add a "Template suggestions" reaction, for details.
With this approach, it's not necessary to write any module code.
function your_theme_preprocess_node(&$variables) {
// Add specific templates.
foreach ($variables['theme_hook_suggestions'] as $theme_suggestion) {
$variables['theme_hook_suggestions'][] = $theme_suggestion . '__' . $node->view_mode;
}
}
also you can call preprocess function here if adding this code
// Run specific preprocess function.
$preprocess = 'your_theme_preprocess_node_' . $node->type . '__' . $node->view_mode;
if (function_exists($preprocess)) {
$preprocess($variables);
}
in this case you can use different templates not only for node types but for different view modes too
I'm creating my own script using the CodeIgniter MVC framework. Now, i want users to easily modify the site functionality and adding their own without modifying the code which i've already written.
How do i make my site pluginable ?
EDIT: The users would be the site admins. Not the end user. Basically just like drupal or joomla. Want the admin to be able to create/add plugins to extend site functionality.
There may be a better way that's specific to CodeIgniter, but this is what I would do:
First, create functions for various "hook points" in your code. Say, a function named PreArticle that you call in your code, before an article is displayed.
Allow the user to write code like this:
addHook_PreArticle('funcToCall');
function funcToCall( &$articleText ) {
$articleText = str_replace('Hello', 'World', $articleText);
}
addHook_PreArticle is a function you've defined, which would add the passed string to some internal list. Then when the PreArticle function is called, each of those functions are executed, passing in any appropriate parameters that you define.
Many CMS's Like Joomla and Blogs like Wordpress use variable function names:
$function="phpinfo";
$function();
You could load this into an array to create a list of functions that can be overridden.
That's a perfect case to use the Observer Pattern.
http://devzone.zend.com/article/5
I'm using CodeIgniter, and will likely use their template library as I want to keep things extremely simple to use. The content for the template variables will come from the database, but I want the business admins to know what content areas are available. Basically the names of the parameters when they choose a specific template. For instance, Joomla uses an extra XML file that defines each area, whereas Wordpress uses comments within a page template to inform the system that the PHP file is a template. I like the Joomla approach because you don't have to parse the PHP file to find the areas, but I like the Wordpress approach because you don't have an extra XML file associated with every template. Are there other approaches that I'm missing?
I think the nicest way would be to add a small hack to the template parser class. The code looks quite readable and clean in system/libraries/Parser.php. You could insert a hook in that class that can be used to keep track of the variables. I don't know, if it works, but here's a snippet:
class CI_Parser {
var $varCallback;
function setVarCallback($callbackFunction) {
$this->varCallback = $callbackFunction;
}
...
function _parse_single(...) {
$callback = $this->varCallback;
$callback($key);
}
...
//Somewhere in your code
function storeVarName($variableName) {
// Persist the variable name wherever you want here
}
$this->parser->setVarCallback('storeVarName');
You could do this directly in the controller:
// in the controller
print_r($data);
$this->load->view("main", $data);
Or a little more rudimentary, but you could pass to the template a PHP array of variables (or an object):
// in the controller
$data = array();
$data["namespace"] = array(
"title" => "My website",
"posts" => array("hi", "something else")
);
$this->load->view("main", $data);
And then in the view, have a flag to print_r the namespace to show all the vars available, so that business admins know exactly what to use.
// in the view
if(isset($namespace["showAllVars"])) print_r($namespace);
One option would be to call token_get_all on the PHP file (only when your business admins are loading it up), and parse the output of that.
The best approach, in my opinion, is to keep the variable definitions in another place (such as a database table, or a separate file). This will help with testing (i.e., a programmer can't just remove a tag and it's gone) and making sure things are still working as you move on with the application development in time.
Another advantage is that your application logic will be independent from the templating engine.
On a side note, if you expect a lot of traffic, you may consider using smarty instead. We have done extensive testing with most of the templating engines around and smarty is the fastest.