Announcement

Collapse
No announcement yet.

Using custom helpers in condition checking for templates?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using custom helpers in condition checking for templates?

    It looks like the PDF/Email custom helper templates must return a string and can't be nested with other helpers, such as the condition checking?

    I'm wanting a block of text to be included only if my custom helper returns true, but variations of the following always run the else block.

    Code:
    {{#ifEqual (customHelperCheck 'someVariable') 'true'}}
    
    This text should render if customHelperCheck returns 'true'.
    
    {{else}}
    
    This text should render if customHelperCheck returns anything besides 'true'.
    
    {{/ifEqual}}
    ​
    The ultimate goal is to include some blocks of text if a quote includes particular product items.

    Any hints?

  • #2
    Hello axyl
    i'm planning to adjust our internal PDF's with additional texts based on values.. I didn't have time to check ALL options, but right now i'll probably go with generating proper text through template helper. So i'll post proper values to template helper and it'll generate proper code which will be displayed on PDF. I'm already doing it in our email templates

    I don't know whether it's the best approach, but i'll probably go with it.

    Comment


    • #3
      Hmm, so you're doing something like...
      Code:
      {{includeConditionalText 'someVariable' 'HTML Text output that you want to be outputted here if the custom helper code says to include the text'}}
      I don't think that'll work for my client as they want to include paragraphs of content/style and will invariably include punctuation and generally look screwy in the editor.

      In handlebars or lightncandy, I should be able to do something like this, but with the hard-coded string result for the custom helpers, it doesn't seem possible here.

      Code:
      {{#customHelperCheck 'someVariable}}
        This content is only rendered if customHelperCheck returns true.
      {{/customHelperCheck}}
      I'm going to dig into the code a bit and see what I can do.

      Comment


      • #4
        I worked it out...

        If your custom helper's render function returns the standard Espo\Core\Htmlizer\Helper\Result type, then the result has to be a string. The getValue function of the returned object is what's passed back to lightncandy and for the standard helper, that's returning a string. Using your own class with its own getValue function, you can return booleans which lightncandy (the handlebars implementation used) then obeys, so you can then use your custom helper function inside conditional code.

        Code:
        {{#if (myCustomHelper "aVariable")}}
        
        The text shown here only displays if itemListIncludes returns true to the if condition.  Otherwise, it's skipped.
        
        {{/if}}
        So I made my helper result class which can be used to return either a safestring or another variable.

        Code:
        class SafeStringOrMixedHelperResult
        {
            private $value = null;
        
            private function __construct() {}
        
            public static function createSafeString(string $value): self
            {
                $obj = new self();
                $obj->value = new SafeString($value);
        
                return $obj;
            }
        
            public static function createEmpty(): self
            {
                $obj = new self();
                $obj->value = '';
                return $obj;
            }
        
            public static function create(mixed $value): self
            {
                $obj = new self();
                $obj->value = $value;
                return $obj;
            }
        
            public function getValue()
            {        
                return $this->value;
            }
        }​
        and then my custom helper's render function returns an instance of that object...

        Code:
            public function render(Data $data): SafeStringOrMixedHelperResult
            {
                // Logic code goes here...
        
        
                return SafeStringOrMixedHelperResult::create(false);  // returning false
            }

        Comment

        Working...
        X