Post to Data (print current username)


I’m using the form plugin to save user generated html from submitted data. Below is the default data.html.twig code that generates these pages in the form plugin.

I’d like to add the current users $username to the page thats generated. Could anyone be so kind as to explain how to go about doing that?

{% for index, field in form.fields %}
{% set input = attribute(field, “input@”) %}
{% if input is null or input == true %}
{% block field %}

{% block field_label %}
{{ field.label }}:
{% endblock %}
{% block field_value %}
{{ string(form.value( ?? index))|nl2br }}
{% endblock %}

{% endblock %}
{% endif %}
{% endfor %}

My overall aim is to later recall the pages that are submitted, for the user to see, using the submitted $username.

If it helps, or is more effeciant I’ve created the twig plugin that allows me to Output some PHP code result in a Twig or md template. which is currently the example string 'something;.

<?php namespace Grav\Plugin; class AddUsername extends \Twig_Extension { public function getName() { return 'AddUsername'; } public function getFunctions() { return [ new \Twig_SimpleFunction('example', [$this, 'exampleFunction']) ]; } public function exampleFunction() { return 'something'; } }

You can use the built-in Twig object grav.user, for example like this <strong>Username:</strong>: {{ grav.user.username}}

Thank you flaviocopes, really appreciate it.

I can’t seem to find that object in the grav documentation. Is there a way to find grav objects like that one? Is it the API classes?:

As in, in the above link can I use group in the same way? (With a known group). If not, how can I find grav objects like this?

Sorry for the newbie question :slight_smile:

No actually that’s added to the Twig variables by the Login plugin, which handles user authentication.

I don’t think it’s documented anywhere, but used in various plugins and themes. Need to be added to the docs.

Ah I see, cheers flaviocopes, Thanks a lot!

Given the above a good go in different ways, but It’s not working:

My Form

{{ config.system.pages.theme }}
my name is: {{ grav.user.username }}

^ The twig above is being processed because the theme is returning antimatter, but the username is returning empty. (My username is set to David on the admin panel.)

Is there something I’ve missed or another option?

EDIT: This is now working.

Added a login to the page and it’s working now. Silly me, I had thought the admin login was already counted and enough.

- site.login: true
admin.login: true