Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Post to Data (print current username)

Started by Muut Archive 10 years ago · 7 replies · 1299 views
10 years ago

Hello,

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 %}
<div>
{% block field_label %}
<strong>{{ field.label }}</strong>:
{% endblock %}
{% block field_value %}
{{ string(form.value(field.name ?&quest; index))|nl2br }}
{% endblock %}
</div>
{% endblock %}
{% endif %}
{% endfor %}
'''

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

10 years ago

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';
}
}

10 years ago

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

10 years ago

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?:

https://learn.getgrav.org/api/Grav/Common/User.html

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 :)

10 years ago

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.

10 years ago

Ah I see, cheers flaviocopes, Thanks a lot!

10 years ago

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?

10 years ago

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.

access:

  • site.login: true
    admin.login: true

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1368 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1125 9 years ago