Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a I get a constant in smarty?

I got a problem with Smarty and constants. I got three constant especified in a file:

DEFINE('ARTICLE_COLOUR_10', 'Light green');
DEFINE('ARTICLE_COLOUR_11', 'Claret'); // Bordó
DEFINE('ARTICLE_COLOUR_12', 'Yellow');

In DB I save only the numbers (10, 11, 12) and I send those numbers through this var

$sql_query_int = mysqli_query($connectdb, "SELECT colour FROM stock WHERE product='$articleId'");    
$smarty->assign('colours', $sql_query_int);

In TPL I get those numbers

{foreach from=$colours key=field item=value}
    {$value.colour}<br>
{/foreach}

Now I want to get the variable ARTICLE_COLOUR_$value.colour; I tried with three different ways but I couldn't get the complete variable.

{$smarty.const.ARTICLE_COLOUR_{$value.colour}}
{$smarty.const.ARTICLE_COLOUR_$value.colour}
{$smarty.const.ARTICLE_COLOUR_value.colour}

Fatal error: Smarty error: [in C:\xampp\htdocs/templates/default/tpl\article.tpl line 10]: syntax error: $smarty.$value.colour is an invalid reference (Smarty_Compiler.class.php, line 2169) in C:\xampp\htdocs\inc\smarty\Smarty.class.php on line 1109

I will be grateful for your help with this problem and forgiveness if this question is misspelled, my English is not very advanced.

like image 783
Marcos Muñoz Avatar asked Nov 05 '16 23:11

Marcos Muñoz


2 Answers

{constant("ARTICLE_COLOUR_{$value.colour}")}
like image 72
sotir zvani djubre Avatar answered Sep 16 '22 22:09

sotir zvani djubre


If you want to get the value of a class constant in a smarty template you can use php constant() function.

<?php

namespace \MyNamespace;

class MyClass
{
    const FOO = "Bar";
}

In the template file:

{constant('\MyNamespace\MyClass::FOO')}

and this outputs the value of the constant.

like image 28
Pavel Petrov Avatar answered Sep 20 '22 22:09

Pavel Petrov