Im trying to pass my own variable from module to template file .tpl
I have this code that is used for displaying availability in eshop. Product.tpl
{if $product.availability == 'available'}
{if $product.quantity <= 0 && $product.allow_oosp}
{if isset($product.available_date) && $product.available_date != '0000-00-00'}
<i class="fa fa-truck rtl-no-flip" aria-hidden="true"></i>
{$product.availability_message}
({if $product.available_date|strtotime > $smarty.now}<span class="available-date">{l s='naskladnění' d='Shop.Theme.Catalog'} {$product.available_date|date_format:"%d.%m.%Y"}</span>{/if})
{/if}
{/if}{/if}
Then Im having my own module where im assigning value to smarty
Mymodule.php
$in_stock = 1;
$this->context->smarty->assign("is_in_stock", $in_stock);
My question is if there is any way to access my smarty variable directly from theme tpl? I need to set up another {if else} with that variable but cant access it.
When I add variable to custom hook I cant access it neither.. Or maybe I dont know how. I tried to create front controller but nothing happened.
Something like
{if isset($product.available_date) && $mymodule.is_in_stock = 1 && $product.available_date != '0000-00-00'}
You can access functions of your module anywhere in .tpl files by
{YourModule::yourFunction()}
and set if like
{if YourModule::yourFunction() != 1}
Hello
{/if}
Related
[PRESTASHOP 1.7] I want to display categories outside of ps_categorytree.
Here is the module code:
$allCategories = Category::getNestedCategories(null, $this->context->language->id);
$this->context->smarty->assign( 'allCategories' , $allCategories );
return $this->fetch('module:'.$this->name.'/views/templates/widget/block.tpl');
.tpl file:
{foreach from=$allCategories item=mainCategory}
{$mainCategory.name}
{foreach from=$mainCategory.children item=subCategory}
{$subCategory.name}
{/foreach}
Error: Notice: Undefined index: link
How do I assign a link?
Because of you got the Category as array, not an object.
Try to use mainCategory['link'] instead of mainCategory.link, and so on
UPDATE
I misunderstood the question, sorry!
Try this.
{foreach $allCategories as $mainCategory}
{$link->getCategoryLink($mainCategory.id_category, $mainCategory.link_rewrite)|escape:'html':'UTF-8'}
{/foreach}
If you have'nt $link variable in the smarty file, you should assign it
'link' => $this->context->link,
I have VPS where I run php (7.4) and nginx.
I have installed Nette and other packages by composer.
My problem is:
I can't print flash messages. In my presenter I have code $this->flashMessage("Odhlášení proběhlo úspěšně.", "success");
And in #layout.latte I have this:
{snippet flashes}
{foreach $flashes as $flash}
{if $flash->type === 'success'}
<script>toastr.success({$flash->message});</script>
{elseif $flash->type === 'info'}
<script>toastr.info({$flash->message});</script>
{elseif $flash->type === 'warning'}
<script>toastr.warning({$flash->message});</script>
{elseif $flash->type === 'error'}
<script>toastr.error({$flash->message});</script>
{else}
<script>toastr.info({$flash->message});</script>
{/if}
{/foreach}
{/snippet}
When I dump $flashes in latte nothing is there.
EDIT:
Flashes save to session but latte can't take it from session.
EDIT 2:
I setting flash message by this way:
In processing form:
$this->flashMessage("Obrázek úspěšně upraven.", "success");
$this->redirect("Gallery:default");
What I have to do to fix this?
Thanks for answers.
I have this in #layout.latte and it works. Show the code of presenter where the flashMassage() is.
{snippet flashes}
<div n:foreach="$flashes as $flash" class="flash {$flash->type}">
{$flash->message}
</div>
{/snippet}
I use this code in Prestashop
{if (strpos($product.name, 'TVNUMBER1') !== false)}
THIS PRODUCT IS IN SALE
{/if}
So whenever I want to display that certain products are in sale, I have to go line by line, specifying the same product i.e."TVNUMBER1". I want to be able to write an array detailing all the products I have in sale "TV1, TV2, TV3", and get a code like this:
{if (strpos($product.name, '$array') !== false)}
THIS PRODUCT IS IN SALE
{/if}
I've tried similar examples found here, but I can't get them to work, either in Prestashop or in PHP testers online. It looks super simple, but I can't get around it.
I think what you want is the in_array php function, that check if a given $needle is or not in an array.
So what you should do is :
{if (in_array($product.name, '$array') !== false)}
THIS PRODUCT IS IN SALE
{/if}
Then in your controller you can assign the array to smarty :
$arr = array('TVNUMBER1', 'TVNUMBER2', 'TVNUMBER3');
$smarty->assign('myArray', $arr);
It seems you are using Smarty as template engine. So you could do something like this (from the doc).
In the controller
//Give it to the view
$arr = array('TVNUMBER1', 'TVNUMBER2');
$smarty->assign('myArray', $arr);
And in the view
//In the view, loop over the array
{foreach from=$myArray item=productName}
//If your product is among the in-sale ones, show the message
{if (strpos($product.name, productName) !== false)}
THIS PRODUCT IS IN SALE
{/if}
{/foreach}
how can I add/print tracking number on invoice?..
I used the code below
{l s='Tracking Number:' pdf='true'}
{$order->shipping_number}
returns blank, but on view order it has a tracking number. (image below)
orders->shipping_number field is blank.
Thanks.
adding on invoice.tpl
{foreach from=$order->getShipping() item=line}
{if $line.url != '' || $line.tracking_number != ''}
{'Tracking Number : '}
{$line.tracking_number}
{/if}
{/foreach}
solves the issue.
I have global function defined in php:
function tabIsProtected($tabNr)
{
...
return true;
}
Now this works in Smarty:
{if tabIsProtected($tabnr)}
{assign var="tabProtected" value=true}
{else}
{assign var="tabProtected" value=false}
{/if}
And this is not:
{assign var="tabProtected" value=tabIsProtected($tabnr)}
When I print $tabProtected variable I get string "tabIsProtected(9)" (for $tabnr = 9) and function tabIsProtected isn't even called!
Is there any way to assign function return value directly in template using Smarty 2?
IMPORTANT: I don't want $smarty->assign(...) in php or Smarty3 solutions. Just one liner directly in template.