pug-php / pug-symfony

Pug (Jade) template engine for Symfony

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using form functions

kingkero opened this issue · comments

Hello & sorry, if an issue is the wrong format, but I just can't get this working.

Switched to pug-symfony in a dev project. So far so good. Using the following in my Controller, coming from plain old Twig.

/**
 * @Route("/", name="homepage", methods={"GET", "POST"})
 */
public function index(Request $request)
{
    $example = new Example();
    $form = $this->createForm(ExampleType::class, $example);

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        // ...
        return $this->redirectToRoute('example_goal', [
        ]);
    }

    return $this->render('foo/index.pug', [
        'form' => $form->createView(),
    ]);
}

How can I display the form in Pug? Tried all of the following

h1 hi
.content
=form_start(form)

h1 hi
.content
= form_start(form)

h1 hi
.content
    =form_start(form)

h1 hi
.content
    = form_start(form)

h1 hi
.content
    =form_start(=form)

How do I get form_start(), form_widget() and form_end() to work in pug-symfony?

commented

If form_start returns HTML, you need to unescape it: !=form_start(form), if you put spaces before =, it means it's inside .content, else it's after. Space after = does not matter, and the last syntax is invalid.

For precise help, always give your expected output (because I cannot guess) and observed output (because I may have a different one).

Finally if you have a Twig snippet, I can give you the exact Pug equivalent and method to "convert".

Sorry for giving so little information. The following is the Twig template I already used

{% extends 'base.html.twig' %}

{% block title %}Hello!{% endblock %}

{% block body %}

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

{% endblock %}

I've been able to rewrite it now to (wheter the form is inside .content or not does not matter for now)

extends ../base

block variables
    - canonical = 'foo'

block prepend title
    | Hello!

block body
    .content
        !=form_start(form)

This now returns (pointing to the line with !=form_start(form))

Error in /.../index.pug:
Function name must be a string on line 11, offset 9

This is a very basic project for now where I simply want to embed a form, as shown here in Symfony's doc.

The variable is passed like so

        $redirect = new Redirect();
        $form = $this->createForm(RedirectType::class, $redirect);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            ...
        }

        return $this->render('create_redirect/index.pug', [
            'form' => $form->createView(),
        ]);
commented

!= is right, (!=x is {{ x }} when = is {{ x | e }} - escaped)

Now form_start is not a global function, form_* are Twig-specific shortcuts and Twig does not provide (as far as I know) a global array of all available functions, so we wrapped a few of them but not all, I will search the helper name to give you the full syntax needed to access form_* methods. And I will re-check if there is nothing new to take all Twig functions at once.

commented

Hi, just to keep you informed, a new version able to support every Twig function is in progress.

@kylekatarnls Awesome to hear. I've been away from Symfony since ~2.4 and just recently started again. Using pug for these projects will be awesome. In the coming year I'll also have some time to maybe contribute

commented

2.6.0 will be released soon and will allow form_* functions and many others.

commented

Version 2.6.0 released.