Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access functions through pug variable? #28

Closed
philliphartin opened this issue Aug 28, 2017 · 4 comments
Closed

How to access functions through pug variable? #28

philliphartin opened this issue Aug 28, 2017 · 4 comments

Comments

@philliphartin
Copy link

I am porting over existing blade templates to pug.
One of the patterns that is used extensively in the default authentication views bundled with the framework is the use of MessageBags, and a global $errors variable that can be accessed in any view.

I wanted to know how this could be converted into a pug template.

{{-- Show the errors, if any --}}
@if ($errors->any())
    <div class="callout">
        <h4>Errors</h4>
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

As you can see, it accesses the errors objects, runs the any() method which returns an array of error messages as strings and then loops over the messages and outputs them to a list.

I have tried the following:

    if errors
        .callout
            h4 Errors
            ul
            each error in errors.all()
               li=error

However, it seems I cannot access the all method in this way. Am I doing this incorrectly?

@philliphartin
Copy link
Author

I actually got it working with errors->all(). Is this how I should be accessing it? Seems a bit non-javascript

@weotch
Copy link
Member

weotch commented Aug 28, 2017

Since you're still in PHP technically, I would expect to work with the php operator, like you discovered. But this is gonnna be the purview of https://github.com/pug-php/pug, we are a wrapper for that package.

@weotch weotch closed this as completed Aug 28, 2017
@philliphartin
Copy link
Author

@weotch Makes sense. Thanks for your help!

@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Aug 29, 2017

Hi @pjhartin We did some quick translate from JS syntax to PHP syntax. It's done by this engine: https://github.com/pug-php/js-phpize

In theory, your code with the dot should work. I tested with the following:
https://pug-demo.herokuapp.com/?embed&theme=xcode&border=silver&options-color=rgba(120,120,120,0.5)&engine=pug-php&input=doctype%20html%0Ahtml(lang%3D%22en%22)%0A%20%20body%0A%20%20%20%20each%20val%20in%20zone.getLocation()%0A%20%20%20%20%20%20p%3Dval%0A&vars=array(%0A%20%20%27zone%27%20%3D%3E%20new%20DateTimeZone(%22Europe%2FPrague%22)%2C%0A)

Each with the dot syntax seems to work in this example. Can you try to update to the last versions with composer update? (According to https://pug-demo.herokuapp.com/, it seems to work since the version 2.1.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants