【问题标题】:How to set a template programmatically using a plugin如何使用插件以编程方式设置模板
【发布时间】:2013-11-18 13:41:15
【问题描述】:

如何使用自定义插件更改后端设置的模板?

我建议使用不同的解决方案

$doc= JFactory::getDocument();
$doc->setTemplate("my_tempalte_name");

这不起作用。

【问题讨论】:

    标签: php joomla joomla3.2


    【解决方案1】:

    在 Joomla 3.2 中。 + 你可以使用JApplicationSite::setTemplate 方法。

    你需要把它放在一个触发onAfterInitialise的系统插件中。

    public function onAfterInitialise()
    {
        $app = JFactory::getApplication();
        // We want to change the template just on the FE
        if ($app instanceof JApplicationSite)
        {
            $template = $app->getTemplate(); //use just debugging
            var_dump($template); //use just debugging
            // Set the new template and style params
            $app->setTemplate('protostar', null);
            $template = $app->getTemplate(); //use just debugging
            var_dump($template); //use just debugging
        }
    
    }
    

    签名JApplicationSite::setTemplate是:

    /**
     * Overrides the default template that would be used
     *
     * @param   string  $template     The template name
     * @param   mixed   $styleParams  The template style parameters
     *
     * @return  void
     *
     * @since   3.2
     */
    public function setTemplate($template, $styleParams = null)
    

    【讨论】:

    • 此方法过去也可以在 Joomla 1.5 中的视图和控制器等组件代码中使用。我刚刚发现它不再适用于 J3.2,但只适用于您上面描述的系统插件,这令人沮丧。知道为什么这会改变吗?组件代码在模板渲染之前执行,所以这似乎是不必要的限制。
    • 自 1.5 以来代码库发生了很大变化,为了更好,我会添加。虽然有些更改乍一看并不一定有意义,但通常是由于需要更好的软件架构而引起的。无论如何,我确实觉得从组件中更改模板也应该可以正常工作(也许那里有一个错误)。随意发送错误报告或仔细查看代码并提交补丁 - Joomla 是开源的,任何人都可以参与改进它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2014-09-22
    相关资源
    最近更新 更多