The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
Custom template tags
How do I add a custom template tags; {vb:tag}.?
|
#2
|
|||
|
|||
I'm not sure because I've never done it, but includes/template_curly_defs.php seems to have a bunch of classes, one for each type of curly tag, all derived from class vB_TemplateParser_Curly (which is also in that file), and the classes are used in includes/class_template_parser.php, with the class name built from the tag name. So maybe if you did something like:
Code:
require_once("includes/template_curly_defs.php"); class vB_TemplateParser_CurlyFoo extends vB_TemplateParser_Curly { public static function validate(vB_Node $main_node, vB_TemplateParser $parser) { // some code } public static function compile(vB_Node $main_node, vB_TemplateParser $parser) { //more code } } then somehow get that included before the template parser is called. (ETA: actually, I'm not sure if you even need to include the base class before defining a subclass, or include the class def before using it). --------------- Added [DATE]1309550427[/DATE] at [TIME]1309550427[/TIME] --------------- Got curious so decided to try it - the answer to both the above questions is "yes", you do need those things defined before they're used. So I used this code at hook admin_global: Code:
require_once("includes/template_curly_defs.php"); class vB_TemplateParser_CurlyFoo extends vB_TemplateParser_Curly { public static function validate(vB_Node $main_node, vB_TemplateParser $parser) { return array(); } public static function compile(vB_Node $main_node, vB_TemplateParser $parser) { return "Foo"; } } and then putting {vb:Foo} in a template resulted in Foo being inserted at that point. Of course the validate function should check the arguments and the compile function will probably do something more complicated, but you can look at the existing classes to figure that out. On a side note, you know what turns out to be a bad idea? Creating a plugin using hook admin_global that has an error in it. I had to go to the database and edit the datastore value before I could get back to the plugin manager. ETA: but now that I think about it, it would have been easier just to temporarily comment out the fetch_hook call. |
#3
|
|||
|
|||
It works.
Can you help with why that I get this error? Code:
Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3433 The following error occurred when attempting to evaluate this template: %1$s This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish. Template: (vbcms_page) HTML Code:
... {vb:raw footer} {vb:jsinline header.js} </body> </html> Template: (header.js) HTML Code:
test Plugin: (JavaScript templates - Hook: admin_global) PHP Code:
Plugin: (Cache javaScript templates - Hook: cache_templates) PHP Code:
--------------- Added [DATE]1309599418[/DATE] at [TIME]1309599418[/TIME] --------------- Yesterday I remove the error without solving the problem by adding to the $output 'Test', but it does not work today. The $output contains comments with the template name. I can only see that it would be the wrong hook to do both things. |
#4
|
|||
|
|||
I was just figuring out the same thing. As it turns out, the compile function is supposed to return php code that results in a string. I think maybe my example worked by accident because a single word is a 'bare word' that gets interpreted as a string. So sorry about that.
So I changed your compile function to be like this: Code:
public static function compile(vB_Node $main_node, vB_TemplateParser $parser) { $output = "'"; foreach ($main_node->attributes AS $template) { if ( strripos($template, '.js') ) { $output .= vB_Template::create($template)->render(); } } return $output . "'"; } and the error goes away, but there's still no output. I get this in the output: Code:
<!-- BEGIN TEMPLATE: headerjs --> <!-- END TEMPLATE: headerjs --> as you pointed out. I don't know know why the '.' is missing from the template name. At this point I'm confused about what's going on. BTW, you probably know that your tag code only runs when the template is compiled, so if you use that code to insert another template then that other template gets changed, it won't be reinserted unless you go back and edit the template with the tag. Maybe what you want to do is something like some of the other curly tag functions do - insert a call to a function that will render the .js template, then it will be called when template with the jsinline tag is rendered. ETA: actually that seems to work, although I don't really understand why. This is what I ended up with: Code:
require_once(DIR . '/includes/template_curly_defs.php'); class vB_TemplateParser_CurlyJsinline extends vB_TemplateParser_Curly { public static function validate(vB_Node $main_node, vB_TemplateParser $parser) { $errors = array(); if (!array_key_exists(0, $main_node->attributes)) { $errors[] = 'no_variable_specified'; } return $errors; } public static function compile(vB_Node $main_node, vB_TemplateParser $parser) { $output = ''; foreach ($main_node->attributes AS $template) { if ( strripos($template, '.js') ) { $output .= 'vB_Template::create(\'' . $template . '\')->render()'; } } return $output; } } |
#5
|
|||
|
|||
Yes you are absolutely right.
If we also look in the file template_curly_defs.php we can see that they all return a php code. If you look after vB_Template and vB_Template_Runtime in file class_core.php it becomes really exciting and I found the solution with the template comment is to send the value true with the render. PHP Code:
Or just use fetch_template_raw. PHP Code:
|
#6
|
|||
|
|||
Can you tell what brings escaped to the lead sentence in header.js?
- I have no problems with header.css template. --------------- Added [DATE]1309718238[/DATE] at [TIME]1309718238[/TIME] --------------- PHP Code:
Tempate: header.css Code:
#quick7{background:url("image.png") no-repeat} Tempate: header.js Code:
var test=""; Tempate: vbcms_page Code:
... <style type="text/css">/*<![CDATA[*/ <!-- {vb:cssinline header.css} --> /*]]>*/</style> </head> <body> ... <script type="text/javascript">/*<![CDATA[*/ <!-- {vb:jsinline header.js} // --> /*]]>*/</script> </body> </html> |
#7
|
|||
|
|||
I'm not sure - are you saying that you need the javascript to be escaped? There is already a class vB_TemplateParser_CurlyEscapeJS class in template_curly_defs.php for escaping javascript, it inserts this:
PHP Code:
|
#8
|
|||
|
|||
Forget about that question.
Can you find a hook for this code? - or equivalent. PHP Code:
You know this code. Hook: admin_global PHP Code:
Quote:
So I have change when to call the template create and it has solved template tags in the named templates and escapes. As you pointed out. --------------- Added [DATE]1309783945[/DATE] at [TIME]1309783945[/TIME] --------------- Answers to your questions above is that there were problems with 'var test="";' was print out as 'var test=\"\";'. But it is solved with the new code. --------------- Added [DATE]1309802492[/DATE] at [TIME]1309802492[/TIME] --------------- Plugin is finished so I close the post. |
Thread Tools | |
Display Modes | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|