Documentation · ViewHelper reference

XSD schema file | vhs master


Try ViewHelper

Attempts to render child content. If an Exception is encountered while rendering, instead the f:else child node is rendered, if it is present. If f:else is not used, no output is returned.

Can be used to perform complex translations of Exception messages which can occur. Can naturally also be used to provide a great deal of additional information about every possible Exception-type error which Fluid can encounter (and there are many).

Note that this is a Condition ViewHelper which means you can use the f:then child node but it differs from regular Conditions by also allowing the template developer to skip the f:then child node and use the direct tag content as the "TRUE" condition and add an f:else which is only rendered in case of an Exception during rendering.

Also note that you can use the then and else attributes; the then attribute is what is attempted rendered and the else attribute is what is rendered if retrieving the then attribute's value fails. Which clearly only makes sense if for example complex inline ViewHelpers are used in the attributes.

Example usage

Please note that this is a theoretical example!

The example is theoretical in one major aspect: v:format.json.decode throws an Exception which Fluid displays as a string always - abstract from this and imagine that a plain Exception happens on errors.

    <!-- assume that the variable {badJson} contains the string "DontDecodeMe"
         which if course is invalid JSON and cannot be decoded. The default
         behavior is to simply output a simple "cannot decode" string. -->
    <v:variable.set name="decodedBadJson" value="{badJson -> v:format.json.decode()}" />
    Displayed only if the JSON decode worked. Much more code and many more
    ViewHelpers can go here. Now, imagine that this block spans so much code
    that potentially there could come an Exception from many additional places
    (for example from Widgets) and you cannot be sure where the Exception comes
    from but still want to tell the user what exactly went wrong and provide
    an error code which makes sense to send to developers if problems persist:
            Error in "{exception.trace.0.class
                -> v:iterator.explode(glue: '_')
                -> v:iterator.pop()
                -> v:format.replace(substring: 'ViewHelper', replacement: ''}"
            <!-- Output example: "Error in Decode <small>1358440054</small>" -->
            <!-- Output example: "The provided argument is invalid JSON" -->
            Value: ``{exception.trace.0.args.0}
            <!-- Output example: "Value: ``DontDecodeMe" which is quite nice
                 for developers to know as part of a bug report from users. -->
...or if you want a shorter version...
<!-- Tries to encode an object, if it fails, falls back by returning a proper JSON
     value, thus preventing breakage by the JSON consumer whatever it may be. -->
{v:try(then: '{badObject -> v:format.json.encode()}', else: '{"validJson": "validValue"')}
<!-- Note: be VERY careful about the inline JSON syntax! It's very close to Fluids. Always
     double quote your object variables' names, that prevents almost all issues! -->


  • Tag usage example

    <v:try as="NULL">
    	<!-- tag content - may be ignored! -->
  • Inline usage example

    {v:try(as: 'NULL')}


Argument Description Type

Template variable name to assign; if not specified the ViewHelper returns the variable instead.

Required attribute

Revision history

commit e88f2f99160d26f5ae222c394282c206228cfd4c
Author: Claus Due <*****>
Date:   Thu Feb 7 00:24:01 2013 +0100

    [FEATURE] Try ViewHelper capable of deep error handling

commit ed15bec7ec1450244979567f31ea9e8b7c3bc374
Author: Claus Due <*****>
Date:   Wed Nov 6 03:21:04 2013 +0100

    [FEATURE] ViewHelperUtility for reusable methods

commit da31bad1d9bb058c072ddb5591e8ed907a1ae8e1
Author: Claus Due <*****>
Date:   Tue Dec 31 23:08:37 2013 +0100

    [TASK] Happy new year!

commit d2e531da034976e6db1478eef5a8478595fd16fe
Author: Claus Due <*****>
Date:   Sun Jan 5 00:45:10 2014 +0100

    [TASK] Migrate to using namespace core class names

commit da7677bc74f14e9a9300e4ba4487e52c3cb97642
Author: Claus Due <*****>
Date:   Sun Apr 20 01:13:33 2014 +0200

    [TASK] Switch to namespaces

commit f16494865266f00f4df185236b7856aa96841039
Author: Xaver Maierhofer <*****>
Date:   Sat Jun 7 22:58:04 2014 +0200

    [TASK] Replace old namespace and move v:if to v:condition and v:var to v:variable in documentaion and readme

commit 12ff0ddfb8d36c51f2d3befde7ae812177c80d5d
Author: Xaver Maierhofer <*****>
Date:   Wed Jun 11 00:01:47 2014 +0200

    [TASK] CGL remove use leading backslash
    PHP Docs
    Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar as opposed to global names that do not, such as FooBar),
    the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.

commit 4981055359c895f3869e5bd878293b3c15ed26e5
Author: Xaver Maierhofer <*****>
Date:   Sun Jan 18 14:01:04 2015 +0100

    [TASK] CGL - Define classes, params, use imports

commit 8bb2fc5ee124ba19b0a0ab8e061c9a153fc4651f
Author: Xaver Maierhofer <*****>
Date:   Sun Jan 18 17:56:13 2015 +0100

    [TASK] Reference Copyright to file