Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

How to add a new internal tag?

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Sat Nov 19, 2011 5:22 pm    Post subject: How to add a new internal tag? Reply with quote

First, let me say I am very excited about Smarty 3. Huge syntax improvement.

That said, I want to create a new tag (internal or external doesn't matter), that looks like this:

{get "some/string" => $result}

I tried accomplishing this by copying the smarty_internal_compile_foreach class, but then realized the rules for expected tokens are built into the parser. I'm totally willing to hack the internal code to make this work, because it's important to my project.

Any help?

Thank you very much!
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Sat Nov 19, 2011 5:51 pm    Post subject: Reply with quote

See http://www.smarty.net/docs/en/plugins.functions.tpl for implementing plugins. However this follows the standard Smarty syntax how attributes are defined.

Do you really need a syntax exact like this:
{get "some/string" => $result}

This would indeed mean to hack the lexer and parser.
Back to top
View user's profile Send private message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Sat Nov 19, 2011 6:20 pm    Post subject: Reply with quote

Thanks for the reply -- really appreciate the support here!

I would *really* like to achieve that syntax. It would be great if it were possible to accomplish custom syntax with custom plugins, but I am willing to hack the lexer and parser if I can figure it out.

However, I suppose the next best option would look like:

{get "some/string" $result}

Which I think we can easily accomplish with shorthand attributes. That brings up another question, is it possible to use shorthand attributes with plain-old custom plugins?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Sat Nov 19, 2011 8:04 pm    Post subject: Reply with quote

The Lexer/Parser is generated by tools which are not part of the normal Smarty distribution.
Messing around with the Smarty syntax can have a lot of side effects. Also upgrading to newer Smarty versions could become an issue.
So I really can't recommend to look into a solution like this.

You can implement shorthand attributes by using compiler plugins.

See http://www.smarty.net/docs/en/plugins.compiler.functions.tpl

I just found out that the documentation is a bit incorrect.
The first parameter is not a string but an array of precompiled attribute values.
Back to top
View user's profile Send private message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Sun Nov 20, 2011 4:11 pm    Post subject: Reply with quote

Ahh that makes sense, thanks for the help!
Back to top
View user's profile Send private message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Sun Nov 20, 2011 7:12 pm    Post subject: Reply with quote

I came across another problem trying to implement this tag:

{post $data to "/uri"}

For some reason "to" is a reserved word? I can use "in" and get no error, but "to" gives me:

Unexpected " to ", expected one of: "}" , " "

Any way around this? Thanks!
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Sun Nov 20, 2011 9:40 pm    Post subject: Reply with quote

yep, to is a reserved keyword in Smarty syntax as it is used in the {for} tag.

No way to get around it.
Back to top
View user's profile Send private message
ericingram
Smarty Rookie


Joined: 16 Nov 2011
Posts: 21

PostPosted: Wed Nov 30, 2011 3:21 pm    Post subject: Reply with quote

Is there any built-in way to create a pre-compiler that would let me get around these compiler rules?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Nov 30, 2011 4:09 pm    Post subject: Reply with quote

Yes, you can implement a prefilter to convert tags of your syntax into valid Smarty tag syntax.

See http://www.smarty.net/docs/en/advanced.features.prefilters.tpl, http://www.smarty.net/docs/en/plugins.prefilters.postfilters.tpl, http://www.smarty.net/docs/en/variable.autoload.filters.tpl, http://www.smarty.net/docs/en/api.load.filter.tpl

Could be the smartest solution for your problem. I did not think about that before.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP