Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable as a parameter in Smarty 5 #997

Closed
LPRKR opened this issue Apr 15, 2024 · 7 comments
Closed

Variable as a parameter in Smarty 5 #997

LPRKR opened this issue Apr 15, 2024 · 7 comments

Comments

@LPRKR
Copy link

LPRKR commented Apr 15, 2024

Can we still pass the variable into the tag parameter in Smarty 5?

In the prior versions, I used it like this and it worked:
{embed type='JS' url='/js/categories.js' cdn=$CDN_URL}

Now, the "cdn" parameter, is equal to the string below instead of the $CDN_URL value:
"$_smarty_tpl->getValue('CDN_URL')"

@wisskid
Copy link
Contributor

wisskid commented Apr 16, 2024

that seems wrong. I'll investigate.

@LPRKR
Copy link
Author

LPRKR commented Apr 18, 2024

I have did some more analysis...
The problem occur when I pass a PHP constant into my custom Tag. It works properly when I pass a string.

Compiled .tpl file result (from cache):
WITH CONSTANT: <link rel="stylesheet" href="$_smarty_tpl->getValue('CSS_URL')"/>
WITH STRING: <link rel="stylesheet" href="css_url_here"/>

Seems like it's missing the <?php echo ... ?> part in this situation.

@wisskid
Copy link
Contributor

wisskid commented Apr 29, 2024

Could you share the code of your implementation for embed?

@LPRKR
Copy link
Author

LPRKR commented Apr 29, 2024

Sure! So, I call it like this:

  1. in .php file - setting the Smarty variable from global variable in PHP
    $app->smarty->assign('JS_TABLEDND', JS_TABLEDND);

  2. in .tpl file - calling extension like this
    {embed type="JS" url=$JS_TABLEDND}

  3. extension code

<?php
namespace SmartyExtensions;
use Smarty\Compile\Base;

class EmbedTag extends Base {
	protected $required_attributes = ['type', 'url'];
	protected $optional_attributes = ['cdn'];
	
	public function compile($params, \Smarty\Compiler\Template $template, $parameter = array(), $tag = null, $function = null):string
	{
		$_attr = $this->getAttributes($template, $params);

		if (!empty($_attr['type']) && !empty($_attr['url']))
		{
			$cdn_loc = '';
			
			if (isset($_attr['cdn']))
				$cdn_loc = $_attr['cdn'];
			
			return \PageElement::embed(trim($_attr['type'], '\'"'), $cdn_loc.$_attr['url'], false, false);
		}
	}
}

If you wonder why I trim the type variable, it's because I tried to make it work with Smarty 5. For some reason the string is surrounded by " characters, that's a different behaviour from Smarty 4.

  1. how it looks on final HTML page:
    <script src="$_smarty_tpl->getValue('JS_TABLEDND')"></script>

@wisskid wisskid removed the needs-fix label Apr 29, 2024
@wisskid
Copy link
Contributor

wisskid commented Apr 29, 2024

Are you sure you want to compile your tag? Because then you cannot use any of the parameters the way you do, because they would no longer be dynamic. Maybe you want to write a FuncionHandler instead?

@LPRKR
Copy link
Author

LPRKR commented Apr 29, 2024

Oh. I tried to follow the documentation but it seems I got it all wrong.
Is there some sample of FunctionHandler usage somewhere? I have few plugins used in 4.x that I need to make work again.

@LPRKR LPRKR closed this as completed Apr 29, 2024
@LPRKR LPRKR reopened this Apr 29, 2024
@wisskid
Copy link
Contributor

wisskid commented Apr 29, 2024

Just have a look at \Smarty\Extension\DefaultExtension::getFunctionHandler and the various function handlers listed there. That should give you enough cloes on how to implement your own function handler.

@wisskid wisskid closed this as completed Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants