Howto write var in style?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
WHK102
Registered User
Posts: 15
Joined: Tue Nov 14, 2017 7:45 pm

Howto write var in style?

Post by WHK102 » Sun Nov 19, 2017 4:56 pm

Have an extension, have a event listener and in style an html code. Howto print in html code the variable writed in listener?

Example, in listener make a random hash, need print the random hash into a javascript variable using overall_footer_after.html.

WHK102
Registered User
Posts: 15
Joined: Tue Nov 14, 2017 7:45 pm

Re: Howto write var in style?

Post by WHK102 » Sun Nov 19, 2017 6:31 pm

In overall_footer_after.html of style of the extension write this:

Code: Select all

<script type="text/javascript">demo = '{CSBM_HASH}';</script>
And in the event:

Code: Select all

static public function getSubscribedEvents()
    {
        return array(
            'core.page_footer_after' => 'page_footer_after'
        );
    }

    public function page_footer_after()
    {
        global $template;
        $template->assign_vars(array(
            'CSBM_HASH' => 'demo'
        ));
    }
But does not work:

Code: Select all

<script type="text/javascript">demo = '';</script>

WHK102
Registered User
Posts: 15
Joined: Tue Nov 14, 2017 7:45 pm

Re: Howto write var in style?

Post by WHK102 » Sun Nov 19, 2017 6:41 pm

Have added the $template from controller but does not work:

Code: Select all

services:
    cspamban.mainlistener:
        class: whk\cspamban\event\main_listener
        arguments:
            ...
            - '@template'

Code: Select all

public function __construct(
        ...
        \phpbb\template\template $template
    )
    {
        ...
        $this->template  = $template;
    }

Code: Select all

static public function getSubscribedEvents()
    {
        return array(
            ...
            'core.page_footer_after' => 'page_footer_after'
        );
    }

Code: Select all

    public function page_footer_after()
    {
        $this->template->assign_vars(array(
            'CSBM_HASH' => 'demo'
        ));
    }
In the ext/whk/cspamban/styles/all/template/event/overall_footer_after.html

Code: Select all

<script type="text/javascript">demo = '{CSBM_HASH}';</script>
The result:

Code: Select all

<script type="text/javascript">demo = '';</script>
What did I do wrong?

User avatar
david63
Jr. Extension Validator
Posts: 13198
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Howto write var in style?

Post by david63 » Sun Nov 19, 2017 7:26 pm

Take a look at my Site Logo extension - that passes vars to js
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

WHK102
Registered User
Posts: 15
Joined: Tue Nov 14, 2017 7:45 pm

Re: Howto write var in style?

Post by WHK102 » Sun Nov 19, 2017 7:51 pm

Uuuhmmm thanks, the differences are:
  • Set the vars when call the heafer of page
    Add espace js to varable
    Add $event as argument to event
I added the points but does not work :(

My changes:

Code: Select all

static public function getSubscribedEvents()
    {
        return array(
            'core.page_header_after' => 'set_header_template_vars'
        );
    }

Code: Select all

public function set_header_template_vars($event)
    {
        $this->template->assign_vars(array(
            'CSBM_HASH' => 'demo'
        ));
    }
Template:

Code: Select all

<script type="text/javascript">demo = '{{ CSBM_HASH | escape('js') }}';</script>
But the result is empty again! D:

Code: Select all

<script type="text/javascript">demo = '';</script>

WHK102
Registered User
Posts: 15
Joined: Tue Nov 14, 2017 7:45 pm

Re: Howto write var in style?

Post by WHK102 » Sun Nov 19, 2017 8:51 pm

I located the error: the extension does not work, the event is not called, my old events canot called D: my extension does not works, i purged the cache but does not works, i put random error by example <?php abc$%&/()='¡ but dont shot the errors... aaahhhhhgggrr! need find the my human error!

Sorry.

User avatar
david63
Jr. Extension Validator
Posts: 13198
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Howto write var in style?

Post by david63 » Sun Nov 19, 2017 9:25 pm

In your template file you should have

Code: Select all

<script>
	var demo = '{{ CSBM_HASH | escape('js') }}';
</script>
and don't forget to load your .js file
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

WHK102
Registered User
Posts: 15
Joined: Tue Nov 14, 2017 7:45 pm

Re: Howto write var in style?

Post by WHK102 » Sun Nov 19, 2017 11:27 pm

I found the problem, have two extensions with same namespace :-/

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 7 guests

cron