smarty

Smarty how to get a first index from foreach?

限于喜欢 提交于 2019-12-07 02:59:21
问题 Construction is this: <!-- projects list --> {if !empty($userObjects)} <select id="projects-list" tabindex="1" name="project"> {if !isset($selected)}<option value="0">Choose project</option>{/if} {foreach from=$userObjects item=v} <option value="{$v.Id}" {if $selected==$v.Id}selected="selected"{/if} }>{$v.Name} {* if it's 1st element *} {if $smarty.foreach.v.index == 0} {if isset($limit)}<br /><span id="projlimit">{$limit}</span> {$currency->sign}{/if} {/if} </option> {/foreach} </select> as

Prestashop custom layout for specific category

纵然是瞬间 提交于 2019-12-07 01:04:46
Can I have a specific category layout for selected categories? The detail page will be the same, I need to get a custom tpl file. Thanks for suggestions Pretty easy to do: override the category controller to set the template. class CategoryController extends CategoryControllerCore { // array with the selected categories private $customCategories = array(1, 3); public function init() { parent::init(); if (in_array($this->category->id, $this->customCategories)) { $this->setTemplate(_PS_THEME_DIR_.'category-custom.tpl'); } } } Here you wouldn't be able to change the selected categories from the

PHP中正则表达式回顾(4)--编写一个非常简单而且山寨的smarty模板引擎

青春壹個敷衍的年華 提交于 2019-12-06 23:42:53
PHP的正则表达式今天就结束了,遥想几年前初次接触的时候,感觉这玩意真心玩不转啊,而时至今日,感觉这也没有什么难以理解的,确实还是有很大进步的,尤其是对smarty模板引擎有了一个更为清晰的认识。正则表达式学到最后,总是会抛出这个编写一个山寨的smarty模板引擎的话题出来练练手,今天就在大师的指导下,编写了这么一个山寨smarty,作为这次复习正则的一个句点吧。 <?php class template{ //存储模板引擎源文件目录 private $templateDir; //编译后的文件目录 private $compileDir; //边界符号 左边界 private $leftTag="{#"; //边界符号 右边界 private $rightTag="#}"; //当前正在编译的模板文件名 private $currentTemp=''; //当前源文件中的html代码 private $outputHtml; //变量池 private $varPool=array(); //构造函数 传入模板文件目录 编译文件目录 public function __construct($templateDir,$compileDir,$leftTag=null,$rightTag=null){ $this->templateDir=$templateDir; $this-

Assigning defaults for Smarty using Object Oriented style

和自甴很熟 提交于 2019-12-06 15:38:24
I'm just very slowly starting to sink into object-oriented programming, so please be gentle on me. I have a custom class for Smarty that was partially borrowed. This is how the only example reflects the basic idea of using it across my current project: class Template { function Template() { global $Smarty; if (!isset($Smarty)) { $Smarty = new Smarty; } } public static function display($filename) { global $Smarty; if (!isset($Smarty)) { Template::create(); } $Smarty->display($filename); } Then in the PHP, I use the following to display templates based on the above example: Template::display(

How to Minify Google AdSense JS?

萝らか妹 提交于 2019-12-06 15:14:40
Whenever I minify the AdSense script I got from Google, it stops working. Any ideas why? Original Code: <script type="text/javascript"><!-- google_ad_client = "xxx"; /* Ad 1 */ google_ad_slot = "2668798369"; google_ad_width = 160; google_ad_height = 600; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> Minified Code: <script type="text/javascript"><!--google_ad_client = "xxx";/* Ad 1 */google_ad_slot = "2338787596";google_ad_width = 200;google_ad_height = 200;//--></script><script type="text/javascript"src="http://pagead2

PHP中substr() mb_substr() mb_struct()的区别和用法及Smar...

孤人 提交于 2019-12-06 12:38:49
PHP substr(),mb_substr()及mb_strcut的区别和用法及Smarty模板中的truncate调节器浅谈 PHP substr() 函数可以 分割文字,但要分割的文字如果包括中文字符往往会遇到问题,这时可以用mb_substr()/mb_strcut这个函 数,mb_substr() /mb_strcut的用法与substr()相似,只是在mb_substr()/mb_strcut最后要加入多一个 参数,以设定字符串的编码,但是 一般的服务器都没打开php_mbstring.dll,需要在php.ini在把php_mbstring.dll 打开。 举个例子: <?php echo mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'); ?> 输出:这样一来我的字 <?php echo mb_strcut('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'); ?> 输出:这样一 从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象…… mbstring 函数的说明: php的mbstring扩展模块提供了多字节字符的处理能力,平常最常用的就是用mbstring来切分多字节的中文字符,这样可以避免出现半个字符的情况

Smarty: is it possible to call a PHP function (from the controller class) inside the template?

試著忘記壹切 提交于 2019-12-06 06:36:43
I created a function in the controller class that needs to be used inside the template (because the template it rendered many times (each template is a post on the website, and they will be all displayed with different values in the newsfeed). Is there a way to call it on a smarty variable? Say you created a function in the controller: public function foo($bar){ $bar++; return $bar; } And then in the template: {$smarbar|foo} or something similar You could register a modifier that will call a given method of a class: class SomeClass { public function someMethod($value) { // return modified

Smarty 'if' statement syntax

旧时模样 提交于 2019-12-06 06:07:25
I have assigned a variable based on the number of characters in a name, which returns an integer (1,2,3, etc). I would like to further add an {if} statement to show an option if the option name matches the variable. The option names are 5" x 7" - 1, 5" x 7" - 2, 5" x 7" - 3 etc. {assign var="numberofcharacters" value=$smarty.get.name|count_characters} {if $vr.variant_name == '5" x 7" - $numberofcharacters' || $vr.variant_name == '8" x 11" - $numberofcharacters'} ... {/if} This is not producing a result, even though I have 3 options that should be showing. Please could somebody let me know what

PHP 安全:过滤、验证和转义

谁说我不能喝 提交于 2019-12-06 05:54:32
我们在开发应用时,一般有个约定:不要信任任何来自不受自己控制的数据源中的数据。例如以下这些外部源: $_GET $_POST $_REQUEST $_COOKIE $argv php://stdin php://input file_get_contents() 远程数据库 远程API 来自客户端的数据 所有这些外部源都可能是攻击媒介,可能会(有意或无意)把恶意数据注入 PHP 脚本。编写接收用户输入然后渲染输出的PHP脚本很容易,可是要 安全 实现的话,需要下一番功夫。我这里以陈咬金的三板斧为引子,给大家介绍三招: 过滤 输入、验证数据,以及转义输出。 1、过滤输入 过滤输入是指转义或删除不安全的字符。在数据到达应用的存储层之前,一定要过滤输入数据,这是第一道防线。假如网站的评论框接受 HTML ,用户可以随意在评论中加入恶意的 <script> 标签,如下所示: <p> 这篇文章很有用! </p> <script>windows.location.href="http://laravelacademy.org";</script> 如果不过滤这个评论,恶意代码会存入数据库,然后在网页中渲染,当用户访问这个页面时,会重定向到可能不安全的钓鱼网站(这种攻击有一个更专业的称呼: XSS 攻击)。这个简单示例很好的说明了为什么我们要过滤不受自己控制的输入数据

Truncate a HTML formatted text with SMARTY

丶灬走出姿态 提交于 2019-12-06 04:06:01
I've got a variable which is formatted with random HTML code. I call it to {$text} and i truncate it. The value is for example: <div>Lorem <i>ipsum <b>dolor <span>sit </span>amet</b>, con</i> elit.</div> If i truncate the text's first ~30 letters, I'll get this: <div>Lorem <i>ipsum <b>dolor <span>sit The problem is, I can't close the elements. So, I need a script, which check the <*> elements in the code (where * could be anything), and if it dont have a close tag, close 'em. Please help me in this. Thanks. Solution after hours, and 4 vote-up @ stackoverflow: PHP: ... function closetags(