十月 CMS:在主题之间共享部分

October CMS: share partial between themes

在主题之间共享部分的简单方法是什么。资产是可能的,但我对整个部分有疑问。我有一个子域 policies/logic 的多主题站点 managed/activated。

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

/**
 * @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);
}

我猜你可以在这里使用 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 它会 affect in all other themes 喜欢它的 shared across multiple themes.

Down side will be that you can not change its content from backend, you need to manually change that file from ftp or other manner.


部分来自另一个主题

您可以添加自定义树枝功能,可能是

In any of your plugin you can add this code

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;
}

现在在你的主题中,当你想使用部分时,你可以使用

{{ theme_partial('call_from_other_theme', 'stemalo' ,{'name':'hardik'}) }}
                                           ^ Your theme name here.

themes/stemalo/partials/call_from_other_theme.htm content

description = "Shared Partial."

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

这样你就可以使用其他主题的部分内容了。

you aksed for access to theme directory here may be you can just you need to pass theme name, and `its dynamic you can pass variable as theme name so it can pick any thing you like.

如有疑问请评论。