Is there a Fluid viewhelper to truncate an URL? If not, how do I make one?

房东的猫 提交于 2019-12-20 06:09:33

问题


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

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

into

www.stackoverflow.com

?

PS: that's the goal:

<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?


回答1:


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.

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" />

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

More details in http://docs.typo3.org/typo3cms/ExtbaseFluidBook/8-Fluid/8-developing-a-custom-viewhelper.html

In TYPO3 ver. 6.x

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

In templates:

{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}" />



回答2:


It's a little bit cumbersome and not very efficient, but it should work and would of course prevent the need for a custom ViewHelper class:

With protocol:
{url -> v:iterator.explode(glue: '/') -> v:iterator.slice(length: 3) -> v:iterator.implode(glue: '/')}
Without protocol:
{url -> v:iterator.explode(glue: '/') -> v:iterator.slice(start: 2, length: 1) -> v:iterator.first()}

Where {url} can come from anywhere, as long as it contains a full http:// or other protocol prefix. The line above of course explodes the URL into parts separated by / then slices off the first three segments and re-joins those using /. Or it simply picks offset 2 (which would be the full domain without protocol) and returns that single element. The result should be an url to the domain of the link, with or without protocol, without trailing slash (which you may or may not want to add yourself after cutting the URL).

Cheers, Claus




回答3:


All ViewHelper are located at typo3/sysext/fluid/Classess/ViewHelper. There are also examples in the header of each file. All ViewHelper of Fedext can be reviewed on the website.



来源:https://stackoverflow.com/questions/20499453/is-there-a-fluid-viewhelper-to-truncate-an-url-if-not-how-do-i-make-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!