简体   繁体   English

如何从Smarty模板构造新对象?

[英]How do I construct a new object from a Smarty template?

What I want to do is this: 我想要做的是:

{myfunc myattribute=new MyClass('arg1', 'arg2')}

But it gives me an error when I try, saying that the function MyClass isn't defined. 但是当我尝试时它给了我一个错误,说没有定义函数MyClass。

The work-around I've been using is to create a top level function like this: 我一直在使用的解决方法是创建一个这样的顶级函数:

function MyClass($arg1, $arg2) { return new MyClass($arg1, $arg2); }

Then in the template doing: 然后在模板中执行:

{myfunc myattribute=MyClass('arg1', 'arg2')}

But is there anyway to avoid creating a new top level function for every class that I want contrastable from Smarty? 但是,无论如何要避免为我希望与Smarty形成对比的每个类创建一个新的顶级函数吗?

I guess I could also create a function that just takes the name of a class to let me construct any class, but that's kind of an ugly solution. 我想我也可以创建一个函数,只需要一个类的名称让我构造任何类,但这是一个丑陋的解决方案。

Edit: To everyone telling my violating best practices, perhaps this will make you feel better: 编辑:每个人都告诉我违反最佳做法,也许这会让你感觉更好:

The object I'm constructing is a widget to render some html. 我正在构建的对象是一个呈现一些html的小部件。 The actual code looks like: 实际代码如下:

{render_widget widget=new MyWidget() id="myid" name="myname"}

Also, I'm unfortunately currently stuck on PHP 5.2, so I cannot use closures. 此外,我很遗憾目前仍然坚持PHP 5.2,所以我不能使用闭包。 I can use the latest version of Smarty3, though I'm currently using a version of Smarty 3.0. 我可以使用最新版本的Smarty3,虽然我目前正在使用Smarty 3.0的版本。

From http://www.smarty.net/best_practices 来自http://www.smarty.net/best_practices

  1. Do not embed PHP 不要嵌入PHP
  2. Keep PHP constructs separate 保持PHP结构分开
  3. Keep business logic separate 保持业务逻辑分离

In your case you mix presentation logic with business logic - and it is bad. 在您的情况下,您将表示逻辑与业务逻辑混合 - 这很糟糕。

However, you can write your custom Smarty plugin (wrapper) for custom objects creation - and (I think) that it is more appropriate "Smarty-way" solution. 但是,您可以编写自定义Smarty插件(包装器)来创建自定义对象 - 并且(我认为)它更适合“Smarty-way”解决方案。

See http://www.smarty.net/docsv2/en/plugins.tpl and http://www.smarty.net/docsv2/en/plugins.functions.tpl http://www.smarty.net/docsv2/en/plugins.tplhttp://www.smarty.net/docsv2/en/plugins.functions.tpl

For example. 例如。

Definition: 定义:

function smarty_function_custom_class($params, &$smarty)
{
    $class = $params['class'];       
    $smarty->assign($params['var'], new $class());  
}    

Usage: 用法:

{custom_class var='myObject' class='MyClass'}

Update 更新

So, if you have such construction: 所以,如果你有这样的结构:

{render_widget widget=new MyWidget() id="myid" name="myname"}

You can change your render_widget in this way: 您可以通过以下方式更改render_widget

function smarty_function_render_widget($params, &$smarty)
{
    $class = $params['widget'];       

    $id = $params['id'];
    $name = $params['name'];

    $widget = new $class($id, $name);

    /**
     *  if you need object in your template, assign it to same variable:
     */
    $smarty->assign($params['widget'], $widget);
    // rest of your code    
}    

And after that you can use it in this way: 之后你可以这样使用它:

{render_widget widget="MyWidget" id="myid" name="myname"}

How about using closures ? 使用闭包怎么样?

{myfunc myattribute=function() { return new MyClass('arg1', 'arg2'); }}

It's not the prettiest thing ever, but it should get the job done. 它不是最漂亮的东西,但它应该完成工作。 (As a side note, you may want to consider switching to the PHP Templating language sometime in the future) (作为旁注,您可能希望将来某个时候考虑切换到PHP模板语言

Unless I'm seriously mis-reading the question you are going about it the wrong way. 除非我严重错误地阅读了这个问题,否则你会采取错误的方式。 Smarty should only hold html and the smarty variables (with very few exceptions). Smarty应该只保存html和smarty变量(极少数例外)。 You'll want to write your class in PHP and instantiate an object there and pass that to smarty. 你会想用PHP编写你的类并在那里实例化一个对象并将其传递给smarty。 Here's an example: 这是一个例子:

Php: PHP的:

class Class_name
{
    private $int;

    public function funtion_name()
    {
        $this->int = 1;
        return $this->int;
    }
}

//instantiate the object  

$obj = new Class_name();

$smarty->('smarty_variable_name', $obj);

Smarty: Smarty的:

$smarty_variable_name->funtion_name()}

This should output 1. 这应输出1。

Also bear with me, I wrote this on my phone :( 也忍受我,我在手机上写了这个:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM