Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Determining "scope" within a compiler tag function

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


Joined: 12 Apr 2013
Posts: 16

PostPosted: Mon Jul 22, 2013 7:19 pm    Post subject: Determining "scope" within a compiler tag function Reply with quote

Is there any way for a compiler tag function to determine whether it is within a "short-hand assignment"?

Example:
{$x = {myCompilerTag}}

Is there any way for the function that implements {myCompilerTag} to determine that its output will be assigned to a variable (x in this case)?

Thanks in advance.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Jul 22, 2013 7:34 pm    Post subject: Reply with quote

The compiler tag does not know in which context it is used.

However if you use a tag as value within another tag Smarty catches the output of the inner tag in an output buffer. The buffer content is then used as value (in your case assigned to $x). Note that the buffer will always be a string.
Back to top
View user's profile Send private message
dpatterson
Smarty Rookie


Joined: 12 Apr 2013
Posts: 16

PostPosted: Mon Jul 22, 2013 7:39 pm    Post subject: Reply with quote

Drat. Crying or Very sad

I'm working with an underlying system that I can't change.
Certain tag implementations need to call a different function if the result will be assigned to a variable.
I'm implementing assign= attributes for most tags, but would also like to let the template designers use short-hand assignments.

I was really hoping that there was some way that I could detect that from within my compiler tag implementation.

Ah well. Such is life.

Thanks for the info.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7237
Location: Lincoln Nebraska, USA

PostPosted: Mon Jul 22, 2013 9:13 pm    Post subject: Reply with quote

You can do this:

{myCompilerTag}
{myCompilerTag assign="x"}

With the 2nd option the output is assigned to template variabe $x. The function then knows if it is being assigned by evaluating the "assign" property, and doing the assigning itself.
Back to top
View user's profile Send private message Visit poster's website
dpatterson
Smarty Rookie


Joined: 12 Apr 2013
Posts: 16

PostPosted: Mon Jul 22, 2013 9:27 pm    Post subject: Reply with quote

Yes, I know I can implement assign tags (is there a way to make it automatic?).

I would like to let the designers use short-hand assignments, though.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7237
Location: Lincoln Nebraska, USA

PostPosted: Mon Jul 22, 2013 9:33 pm    Post subject: Reply with quote

How would you remedy that in PHP?

myFunc();

$x = myFunc();

So somehow myFunc() behaves differently if its output gets assigned? No can do. This sounds like an implementation issue to rework, it should not be the responsibility of the func being called. But I don't know the scope of what you are trying to accomplish either.
Back to top
View user's profile Send private message Visit poster's website
dpatterson
Smarty Rookie


Joined: 12 Apr 2013
Posts: 16

PostPosted: Mon Jul 22, 2013 10:01 pm    Post subject: Reply with quote

As I mentioned in my original post, I'm working with an older system.
My tags need to call functions in that system.
There are many cases where there is a my_whatever() function and a get_my_whatever() function.

The former displays the whatever and the latter returns the whatever.
In some cases, they don't do exactly the same thing so I have to call the correct one based on context.

Hence the request for a flag/status method that would indicate that the compiler tag is being called within the context of a short-hand assignment
(http://www.smarty.net/forums/viewtopic.php?t=24445).
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7237
Location: Lincoln Nebraska, USA

PostPosted: Tue Jul 23, 2013 2:47 am    Post subject: Reply with quote

Can you make all the functions in the older system always return the content? Then the calling operation can decide if the data is displayed or not. No different than PHP.

Code:
function sayHello() { return "hello"; }

// show it
echo sayHello();

// save it
$output = sayHello();
Back to top
View user's profile Send private message Visit poster's website
dpatterson
Smarty Rookie


Joined: 12 Apr 2013
Posts: 16

PostPosted: Tue Jul 23, 2013 4:38 am    Post subject: Reply with quote

Unfortunately, changing the old system is out of the question.
I have to work within those boundaries.
Even if I decided to try, the "process" to approve and release such changes could take longer than
getting Smarty changed Wink or coming up with a work around.
It would also introduce backwards compatibility issues for other projects that rely on the old system.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7237
Location: Lincoln Nebraska, USA

PostPosted: Tue Jul 23, 2013 1:50 pm    Post subject: Reply with quote

then maybe your last resort is to wrap things into {capture}{/capture} and buffer the output of the functions that echo content.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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