我有一个类,它包括Smarty,但我的类使用命名空间测试,Smarty不使用命名空间.如何包括Smarty,而无需将名称空间写入智能文件(它有许多系统插件)
import "smarty/Smarty.php"
class testik
{
public function __construct ()
{
$smarty = new Smarty();
}
}
<?php
class Smarty
{
//somcode
}
Run Code Online (Sandbox Code Playgroud)
Smarty有自动加载器类并包含其插件,插件也没有命名空间.
在Smarty中,可以通过这种方式注册插件:
$smarty->registerPlugin("function","date_now", "print_current_date");
function print_current_date($params, $smarty)
{
if(empty($params["format"])) {
$format = "%b %e, %Y";
} else {
$format = $params["format"];
}
return strftime($format,time());
}
Run Code Online (Sandbox Code Playgroud)
参考:https://www.smarty.net/docs/en/api.register.plugin.tpl
但我正在寻找一种方法,我可以直接传递函数作为参数.我怎么能在PHP/Smarty中做到这一点?
例如:
$smarty->registerPlugin("function","date_now", function ($params, $smarty) {
if(empty($params["format"])) {
$format = "%b %e, %Y";
} else {
$format = $params["format"];
}
return strftime($format,time());
});
Run Code Online (Sandbox Code Playgroud) 我正在开发一个基于PHP的应用程序,需要国际化(i18n)支持.
我正在使用Smarty 3作为模板解决方案(以及TinyMVC作为框架),虽然我之前一直在将它用于小型项目(早在Smarty 2.x时代),但我还不知道任何简单 - 使用i18n插件.Smarty 3.x是否有这样的插件/扩展程序可以帮我完成这项工作?我不介意后端是否有点难以设置,这很好,我需要的只是在模板中实现它的好方法.
我需要将这个应用程序翻译成多种语言,所以我需要找到一种方法让Smarty支持它.有吗?谢谢.
我很久以前就使用过PHP5,目前我正在尝试更新我的知识,并在Zend Framework 2和Smarty 3的知识中为可能的项目增加知识.
我目前正试图弄清楚如何使用Smarty 3作为Zend Framework 2的替代/补充.
我可以通过Google在此主题上找到的所有内容:
可有人请向我解释,就好像我是9,使用Zend主干应用程序作为我的出发点:
在什么目录/文件夹(使用完全限定的路径),我应该解压缩Smarty-3.1.12包?
我需要创建哪些类,它们究竟需要包含哪些类,以及它们应存储在何处?
需要修改哪些文件,以及如何使Smarty可以在我的模块中查看?
参考上面创建的类,如何从AZ中使用这些类(例如,包括在适当的类中,分配值,创建输出)?
我的模板文件中有以下代码:
{foreach from=$items item=entry}
<pre>
{$entry->nb_persons|@print_r}
</pre>
{/foreach}
Run Code Online (Sandbox Code Playgroud)
输出是(json字符串):
{"ip":"12.12.12.12","date":1375616434,"cartitems":["foo:1"],"company":"dsad","FirstName":"sad","LastName":"asdsad","street":"","postcode":"","city":"","country":"Andorra","phone":"456456","fax":"","email":"sad@sad.com","comefrom":"google","request":"","message":"sadads"}
我想打印每个分开的元素,例如:
{$entry->nb_persons.company}
应该给我 - >"dsad"
但这不起作用,我不知道为什么.
我遇到了这个问题:没有选择数据库.我翻过这里发布的相同问题,但经过几个小时的阅读后,我无法弄清楚为什么没有选择数据库.我创建了一个数据库job和一个表job.我用WAMP服务器运行脚本.对"日常问题"感到抱歉.请帮忙!
<?php
// load Smarty library
require('C:/wamp/www/smarty-3.1.21/libs/Smarty.class.php');
$servername = "localhost";
$dbname = "job";
// Create connection
$conn = mysqli_connect($servername, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$smarty = new Smarty;
$smarty->setTemplateDir('C:\wamp\www\app\templates');
$smarty->setCompileDir('C:\wamp\www\app\templates_c');
$smarty->setConfigDir('C:\wamp\www\app\configs');
$smarty->setCacheDir('C:\wamp\www\app\cache');
$rows = array();
$sql = "SELECT * FROM job";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo 'MySQL Error: ' . mysqli_error($conn);
exit;
}
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$smarty->assign('output', $rows);
$smarty->display('result.tpl'); …Run Code Online (Sandbox Code Playgroud) 我找不到解决方案.我要做的就是翻译这个:
se stai inserendo un indirizzo per consegna all'interno dell'area <span class=orange>EXPO</span>
Run Code Online (Sandbox Code Playgroud)
进入这个:
If your delivery address is in the <span class=orange>EXPO</span> area
Run Code Online (Sandbox Code Playgroud)
如果我使用标签,像这样:
{l s="se stai inserendo un indirizzo per consegna all'interno dell'area <span class=orange>EXPO</span>"}
Run Code Online (Sandbox Code Playgroud)
他们不会被看到.所以呢?
我的代码如下:
content.tpl:
{* Smarty *}
{extends file='PageContentLayout.tpl'}
Run Code Online (Sandbox Code Playgroud)
PageContentLayout.tpl
{* Smarty *}
{block name="file_name"}
<p>{$smarty.current_dir}</p>
<p>{$smarty.template}</p>
{/block}
{block name="other_content"}
...
{* blah... *}
...
{/block}
Run Code Online (Sandbox Code Playgroud)
在早期版本的smarty中,此代码将打印模板名称和文件路径:content.tpl.
但是,我刚刚升级到3.1.29,现在看来它PageContentLayout.tpl是正在打印的基本文件的名称.
我认为这是Smarty的不同版本中的故意设计更改,但我找不到有关这些更改的任何文档.
我真正想知道的是,实现前一功能的最佳方法是什么?
==编辑==
我注意到,调用,即使{$smarty.current_dir}从延长荷兰国际集团的文件,我们仍然得到基本文件的路径和文件名.这是与早期版本相比的一个重大变化,在我的情况下非常严重,因为我不能再编写动态代码来查找顶级文件的路径.
我必须计算相对于今天的销售结束时间,
我有这种格式的销售结束时间变量,{%d/%m/%Y %H:%M:%S} 例如:(31/07/2016 18:27:58).
我要做的是展示ends: 8 days 06:10:29.
我不能在模板中使用php代码,因为我使用的是smarty 3而且我不是smarty的专家.
我正在尝试从旧的 PHP 5.4.45 + Smarty 2.6.28 迁移到新的 PHP 7.2.25 + Smarty 3.1.33。
我正在调整一些小代码片段,它在大多数网页中都可以工作,但突然在某些 PHP 页面中停止工作,返回以下错误消息:
Notice: Undefined property: Smarty_Internal_Undefined::$objMap in /home/www/libs/Smarty-3.1.33/libs/sysplugins/smarty_internal_extension_handler.php on line 132
Fatal error: Uncaught --> Smarty: undefined extension class 'Smarty_Internal_Method_Get_Template_Vars' <-- thrown in /home/www/libs/Smarty-3.1.33/libs/sysplugins/smarty_internal_undefined.php on line 62
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚哪个 PHP 代码导致了此错误消息,因为我在其中没有看到任何要分析的行位置。看起来这些页面的 PHP 代码与正常运行的页面非常相似。我在网上看到这个错误可能是由PHP中的“register_object”引起的。我刚刚检查了我的 PHP 文件,其中没有“register_object”。
在不工作的 PHP 页面(如工作页面)中,除了其余的 PHP 操作之外,我还执行以下操作:
$myTemplate = new Smarty();
$myTemplate->compile_dir = $mypath . "/templates_c";
$myTemplate->template_dir = $mypath . "/templates";
$myTemplate->assign("myvarible", $foo);
$myTemplate->display("mytemplate.html");
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。非常感谢。
smarty3 ×10
php ×9
smarty ×6
prestashop ×2
include ×1
json ×1
localization ×1
mysql ×1
namespaces ×1
templates ×1
translation ×1