首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >10月CMS:主题间共享部分

10月CMS:主题间共享部分
EN

Stack Overflow用户
提问于 2019-01-15 20:39:30
回答 1查看 509关注 0票数 0

主题之间共享部分的简单方法是什么?资产是可能的,但我对整个部分有异议。我有一个由子域策略/逻辑管理/激活的多主题站点。

解决方案/**在此组件的上下文中呈现请求的部分,*请参阅Cms\Classes\Controller@renderPartial以了解使用情况。*/

代码语言:javascript
复制
/**
 * @param $themeName
 * @param $partialName
 * @param $data
 * @return mixed
 * @throws \Cms\Classes\CmsException
 */
public function renderThemePartial($partialName, $themeName, $data)
{
    $theme = Theme::getActiveTheme();
    if($themeName) {
        $theme = Theme::load($themeName);
    }

    $controller = new Controller($theme);

    return $controller->renderPartial($partialName, $data);
}

/**
 *
 * Renders a requested content in context of this component,
 * see Cms\Classes\Controller@renderContent for usage.
 */

/**
 * @param $themeName
 * @param $contentName
 * @param $data
 * @return string
 * @throws \Cms\Classes\CmsException
 */
public function renderThemeContent($contentName, $themeName, $data)
{
    $theme = Theme::getActiveTheme();
    if($themeName) {
        $theme = Theme::load($themeName);
    }

    $controller = new Controller($theme);

    return $controller->renderContent($contentName, $data);
}


public function registerMarkupTags()
{
    return [
        'functions' => [
            'partial_from_theme' => [$this, 'themePartial'],
            'content_from_theme' => [$this, 'themeContent'],
        ],
        'filters' => [
            'theme_asset'   => [$this, 'themeUrl']
        ]
    ];
}

/**
 * @param $requested
 * @return string
 */
public function themeUrl($requested)
{
    $asset = $requested[0];
    $theme = $requested[1];
    $theme = Theme::load($theme);
    $themeDir = $theme->getDirName();
    if (is_array($asset)) {
        $_url = Url::to(CombineAssets::combine($asset, themes_path().'/'.$themeDir));
    }
    else {
        $_url = Config::get('cms.themesPath', '/themes').'/'.$themeDir;
        if ($asset !== null) {
            $_url .= '/'.$asset;
        }
        $_url = Url::asset($_url);
    }
    return $_url;
}

/**
 * @param $partialName
 * @param null $themeName
 * @param array $parameters
 * @return mixed
 * @throws \Cms\Classes\CmsException
 */
public function themePartial($partialName, $themeName = null, $parameters = [])
{
    return $this->renderThemePartial($partialName, $themeName, $parameters);
}

/**
 * @param $contentName
 * @param null $themeName
 * @param array $parameters
 * @return string
 * @throws \Cms\Classes\CmsException
 */
public function themeContent($contentName, $themeName = null, $parameters = [])
{
    return $this->renderThemeContent($contentName, $themeName, $parameters);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-16 16:52:37

我想您可以在这里使用Plugin component,您可以使用add component to pageuse its partial

您需要在组件中定义部分[ you can add as many as you like ]

然后像下面这样使用页面中的partial,现在是this same thing you can do in other themes as well.

当你change partial的时候,它会像它的shared across multiple themes.一样

Down side将是您的can not change its content from backend,您需要手动从ftp或其他方式更改该文件。

来自另一个主题的部分

您可以为它添加自定义的小枝函数

在任何一个插件中,您都可以添加以下代码

代码语言:javascript
复制
public function registerMarkupTags()
{
    return [
        'functions' => [                
            'theme_partial' => [$this, 'themePartial'],
        ]
    ];
}

public function themePartial($partialName, $themeName = null, $vars = [])
{
    $theme = Theme::getActiveTheme();
    if($themeName) {
      $theme = Theme::load($themeName);
    }
    $template = call_user_func([Partial::class, 'load'], $theme, $partialName);
    $partialContent = $template->getTwigContent();
    $html = Twig::parse($partialContent, $vars);
    return $html;
}

现在,在主题内部,当您想要使用部分时,您可以使用

代码语言:javascript
复制
{{ theme_partial('call_from_other_theme', 'stemalo' ,{'name':'hardik'}) }}
                                           ^ Your theme name here.

themes/stemalo/partials/call_from_other_theme.htm含量

代码语言:javascript
复制
description = "Shared Partial."

[viewBag]
==
<div id="shared_partial">
    <h1>Another Theme Partial : {{name}}</h1>
</div>

这样,您可以使用来自另一个主题的部分。

这里的you aksed for access to theme directory --您只需传递主题名就可以了,并且‘它的动态您可以将变量作为主题名传递,这样它就可以选择您喜欢的任何东西。

如有任何疑问,请评论。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54206477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档