Jawsh
4 years ago

Templating - content checks

Posted 4 years ago by Jawsh

XenForo, on the Zend framework, has templating options that looks something like this:

<xen:if hascontent="true">
<ul>
    <li>This is a header!</li>
    
    <xen:contentcheck>
    <xen:if is="$user.admin">
        <li>This is content!</li>
    </xen:if>
    </xen:contentcheck>
</ul>
</xen:if>

The xen:if is asking if that particular context has content, which is determined by non-whitespace characters being present in the xen:contentcheck. In the code I gave, the content check would only pass if $user.admin were true. If not, the entirety of the content in the original xen:if would not pass, including the <ul> tag and <li> tag.

So, I have a similar situation in Laravel where I have a list of moderator tools that render on a variety of very complicated permissions.

<ul>
    @if ($user->canDoSomething())
    <li><button>Do something about it</button></li>
    @endif
    
    @if ($user->canDoSomething2())
    <li><button>Do something about it</button></li>
    @endif
    
    @if ($user->canDoSomething3())
    <li><button>Do something about it</button></li>
    @endif 
</ul>

In this scenario, in order to properly display nothing if no actions are available, I would need to put an @if statement at the top that tests all 3 things before showing the menu, or, I would need to make a $user->canDoAnything() that tests all 3 things for me. Neither are really pretty ways of doing it.

So, is there any way of doing content checks? I will also accept something like this.

@section('actions')
<ul>
    @if ($user->canDoSomething())
    // set a variable true here
    <li><button>Do something about it</button></li>
    @endif
</ul>

@if (isset($variableSetPreviously))
    @show
@else
    @stop
@endif

But that's also less-than-perfect and kind of a hack.

Please sign in or create an account to participate in this conversation.