【问题标题】:get template variable value on each page modx revolution在每个页面 modx 旋转上获取模板变量值
【发布时间】:2015-05-07 20:48:18
【问题描述】:

我使用 modx,老实说,我是这个 CMS 的新手。我想在每一页上显示相应的模板变量的值。这是我用 sn-p 编写的代码:

<?php
if ( isset($modx->documentObject['PDF-Resource-Url'][1]) && !empty($modx->documentObject['PDF-Resource-Url'][1]) ) {
echo '<li class="related-link slide expanded"><a href="' . $modx->documentObject['PDF-Resource-Url'][1] . '" target="_blank">Pdf</a></li>';
}
?>

但它返回空输出。我使用最新版本的 modx 革命。我认为这可能是我看到空输出的原因。

感谢您的帮助!

【问题讨论】:

    标签: php modx modx-revolution


    【解决方案1】:

    看起来您正在使用来自 MODX Evolution 的方法。革命是不同的,所以熟悉文档是个好主意。我在下面提供了一些链接。

    使用 API 获取模板变量值很简单:

    $value = $modx->resource->getTVValue('tv-name');
    

    $modx-&gt;resource 始终包含当前资源的对象。

    你的例子会变成:

    $output = '';
    
    $url = $modx->resource->getTVValue('PDF-Resource-Url');
    if (!empty($url)) {
        $output = '<li class="related-link slide expanded"><a href="' . $url . '" target="_blank">Pdf</a></li>';
    }
    
    // always return output rather than echoing to page
    return $output;
    

    如何获取当前资源对象: http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/structuring-your-site/resources

    如何检索模板变量: http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/accessing-template-variable-values-via-the-api

    基本的sn-p开发: http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets

    【讨论】:

    • 非常感谢@okyanet 的回答
    猜你喜欢
    • 2016-03-10
    • 2021-04-25
    • 2013-03-05
    • 2016-10-03
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    相关资源
    最近更新 更多