View previous topic :: View next topic |
Author |
Message |
dpatterson Smarty Rookie
Joined: 12 Apr 2013 Posts: 16
|
Posted: Mon Jul 22, 2013 7:19 pm Post subject: Determining "scope" within a compiler tag function |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Jul 22, 2013 7:34 pm Post subject: |
|
|
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 |
|
dpatterson Smarty Rookie
Joined: 12 Apr 2013 Posts: 16
|
Posted: Mon Jul 22, 2013 7:39 pm Post subject: |
|
|
Drat.
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Jul 22, 2013 9:13 pm Post subject: |
|
|
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 |
|
dpatterson Smarty Rookie
Joined: 12 Apr 2013 Posts: 16
|
Posted: Mon Jul 22, 2013 9:27 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Jul 22, 2013 9:33 pm Post subject: |
|
|
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 |
|
dpatterson Smarty Rookie
Joined: 12 Apr 2013 Posts: 16
|
Posted: Mon Jul 22, 2013 10:01 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Jul 23, 2013 2:47 am Post subject: |
|
|
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 |
|
dpatterson Smarty Rookie
Joined: 12 Apr 2013 Posts: 16
|
Posted: Tue Jul 23, 2013 4:38 am Post subject: |
|
|
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 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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Jul 23, 2013 1:50 pm Post subject: |
|
|
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 |
|
|