0

I'm using CakePHP 2.x

I have a couple elements that require javascript files, so inside the element ctp file (whatever.ctp) I do something like this: $this->Html->script('whatever.js', array('inline'=>false)); This normally works great...

But I noticed that when I try to use this exact same element directly on a layout (not a view) it doesn't work:

//inside default.ctp layout echo $this->element('whatever');

The element shows up normally, but the scripts do not get added to the script block. If I move that same element into a view, the scripts do get added. Or if I take the scripts out of the element and just add $this->Html->script('whatever.js', array('inline'=>false)); to the default. That also works.

So why can't elements add to the script block outside of a view?

In case it's not clear...

//this works
layout -> view -> element -> $this->Html->script()

//this works
layout -> $this->Html->script()            

//this DOESN'T work
layout -> element -> $this->Html->script()
2
  • Where is the echo when using inline=>true? Commented Dec 30, 2014 at 22:59
  • @mark I corrected the code in the question. It should have been 'inline'=>false. Whether I set it to true or false, no script tag appears (when $this->Html->script() is inside the element that's used in the layout). Commented Dec 31, 2014 at 0:33

1 Answer 1

1

It does ALL work as long as you didnt output the scripts yet. You need to understand the execution order. Views first, then layout. Once the layout renders, and once you output the buffered content, you cannot use buffered output anymore (using inline false). You would THEN need inline true and directly echo it.

Sign up to request clarification or add additional context in comments.

7 Comments

Makes sense what you said about the execution order. But why doesn't inline=true do nothing also? And is there a workaround to allow my elements to work properly in the layout as well as view? Because the scripts get rendered in the <head> basically any element used in the layout will come after.
Did you use "echo" with inline=>true as I outlined above in the commend?
Ahhh... I misunderstood the question! I thought you were asking where the <script> was echoed on the page. You were pointing out that we must use "echo" if inline = true. I thought it was automatic. Good catch.
Do you suggest echoing the scripts inline as a workaround for using the element directly in the layout? I normally try not to do that for performance reasons, but it's not the end of the world. But I'm pretty sure that the default $option['once']=true will get ignored also if inline=true, and that opens the door to repeated scripts... which would not be ok.
Then make sure you write all scripts to the buffer prior to outputting them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.