是否有Fluid Viewhelper来截断URL?如果没有,我该如何做一个? [英] Is there a Fluid viewhelper to truncate an URL? If not, how do I make one?

查看:66
本文介绍了是否有Fluid Viewhelper来截断URL?如果没有,我该如何做一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TYPO3的Fluid或Fedext/vhs中,是否有可以转换的viewhelper

In TYPO3's Fluid or in Fedext/vhs, is there a viewhelper that can convert

http://www.stackoverflow.com/questions/ask

进入

www.stackoverflow.com

?

PS:这是目标:

<f:format.raw><f:link.external uri="{item.link}">{item.just-display-the-domain}</f:link.external></f:format.raw>

编辑(使问题适应我得到的答案):如果必须构建自定义视图帮助器,该如何进行?

EDIT (adapting the question to the answer I got): If I have to build a custom view helper, how do I proceed?

推荐答案

我真的怀疑是否有任何合理的理由将这种VH添加到核心中,事实上,编写自定义VH就像小菜一碟(当您最终意识到它是)时,开发人员可以在几分钟内在其自定义工具exts中创建简单的格式化程序.

I really doubt if there would be any sensible reason for adding this kind of VH into the core, de facto, writing custom VH is like a piece of cake (when you finally realize it is) so simple formatters can be created by devs in their custom tool exts just in minutes.

即.在TYPO3 4.x中,假设您有一个自定义扩展,其扩展键为urs,那么您所要做的就是创建一个适当的类,该类包含render($params)方法并扩展Tx_Fluid_Core_ViewHelper_AbstractViewHelper类:

Ie. in TYPO3 4.x assuming that you have a custom extension with key urs all you need to do is create one proper class, containing render($params) method and extending Tx_Fluid_Core_ViewHelper_AbstractViewHelper class:

/typo3conf/ext/urs/Classes/ViewHelpers/GetDomainViewHelper.php:

<?php
class Tx_Urs_ViewHelpers_GetDomainViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {

    /**
     * @param $link string Each `allowed` param need to have its line in PHPDoc
     * @return string
     */
    public function render($link) {
        $link = str_replace('http://', '', $link);
        $link = str_replace('https://', '', $link);
        $segments = explode('/', $link);
        return trim($segments[0]);
    }
}
?>

在您的模板中声明其名称空间,然后...就可以使用它:

Next in your templae declare its namespace and... that's all, you can use it:

{namespace urs=Tx_Urs_ViewHelpers}

<urs:getDomain link="http://stackoverflow.com/questions/20499453" />

在诸如Tx_Urs_ViewHelpers...之类的东西中要特别注意字母大小写.

Take special attention about letter case in things like Tx_Urs_ViewHelpers... etc.

http:中的更多详细信息://docs.typo3.org/typo3cms/ExtbaseFluidBook/8-Fluid/8-developing-a-custom-viewhelper.html

事物的工作方式相似,主要的变化是新的命名空间

Things works preaty similar the main change of course is new namespacing

/typo3conf/ext/urs/Classes/ViewHelpers/GetDomainViewHelper.php:

<?php
namespace TYPO3\Urs\ViewHelpers;

class GetDomainViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

    /**
     * @param $link string Each `allowed` param need to have its line in PHPDoc
     * @return string
     */
    public function render($link) {
        $link = str_replace('http://', '', $link);
        $link = str_replace('https://', '', $link);
        $segments = explode('/', $link);
        return trim($segments[0]);
    }
}

在模板中:

{namespace urs=TYPO3\Urs\ViewHelpers}

<urs:getDomain link="http://stackoverflow.com/questions/20499453" />

当然,在两种情况下,您将使用:

Of course in both cases instead of using hardcoded links you will use:

<urs:getDomain link="{item.link}" />

这篇关于是否有Fluid Viewhelper来截断URL?如果没有,我该如何做一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆