Place static javascript code on all pages - magento

Im new to Magento. I would like to know how can I place static javascript code across all pages of the magento site. The JS code does some initilization and includes another hosted js file. Also, I would like to add this code in footer of all pages.
Any guidance/docs/codes welcomed.
Thnx in advance...

The fastest way is to create a new file in the "js" folder in the root
of your website. To include the Javascript file in the code, go to
System -> Configuration -> Design -> Footer -> Miscellaneous Scripts
And add your script there like you would normally do.
There are other ways, but this is the easiest way to do it.
Edit: Changed HTML head to Footer, thanks dagfr
- Edit -
The above example is not what he meant, he wants to add JS to the footer, so when he distributes the module, the JS will also be in the footer.
Answer
You can use the layout .xml file for your module to add a JS file.
To do this, you can add the following code:
<action method="addItem"><type>js</type><name>script_name_here.js</name></action>
In order to place it in the footer, you will have to make a new block, which is explained in the following article.

Related

magento 2 custom phtml page

Having just got myself acquainted (enough) with Magento 1.9, and able to make the customisations required, I've been told that once it's out, we're moving to Magento v2.0.
Having found the differences in the file structure, I believe I can see where to place my code for the custom pages we use, but how can I add this page from my project to a static block for later use?
Previously, the method used was as follows (I realise this may not follow the best practise, but it worked):
Create element folder within theme or core templates directory.
eg. /app/design/frontend/<theme>/default/template/myelement/mypage.phtml
Add this page/element to a static block using the following in the content editor:
{{block type="<theme>/default" template="myelement/mypage.phtml"}}
This block can then be added to the category pages as required.
In Magento 2, I have tried what I believe to be required, which is replicating the file structure and adding phtml files to this, so the template file now resides in:
/app/code/<supplier>/<module>/view/frontend/templates/mypage.phtml
Adding to the content editor the following:
{{block type="<supplier>/<module>" template="mypage.phtml"}}
Unfortunately, this does not display the intended page (element). It doesn't display the calling "{{block" entry either, which usually happens when the line is invalid, so I can only assume that I'm missing something with the link to this template.
If anyone can offer some assistance with this, I would be most grateful.
EDIT:
Continued research on this has led me to the following assumptions:
Magento 2 requires more than just a new .phtml page, even for simple customizations.
I'm still missing something.....
Having gone through 3 different tutorials on creating new modules for Magento 2, each providing slightly different methods, but fundamentally being the same thing, I now have what I believe should be all the code elements to make a new .phtml template for display in a static block.
This has led to an additional problem though.
While I have the required code, I cannot add the module. Adding the module to etc/config.php, as suggested in 2 out of the 3 tutorials, simply crashed M2, both admin and frontend when you try to clear the cache. This is the case after manually clearing cache folders in the var directory.
Also, still unable to add the .phtml template file to a static block or page using content editor.
Not much hair left to pull out here, so looking for help! Thanks in advance
There are a few samples on GitHub, including https://github.com/magento/magento2-samples/tree/master/sample-module-newpage which shows a module adding a new page with a very simple PHTML template file. This example does not use CMS content editing however - it is a sample based on using layout files.
You mentioned you were getting crashes. Would need more details to help on that one. If you got it solved, could you update this question and accept a response to close it out? Thx!
You Should try "class" instead of "type". So your code should look like.
{{block class="<package>\<module>\Block\MyBlock" template="mypage.phtml"}}
You Should try "VenderName_Modulename::myelement/mypage.phtml" instead of "myelement/mypage.phtml". So your code should look like.
{{block class="VenderNameModulename\Block\MyBlock" template="VenderName_Modulename::myelement/mypage.phtml"}}

Magento: Update several pages with a different tracking code

I have to put some Tracking code on several pages: some category pages, homepage, cart and success page. Whats the best way to accomplish that?
I thought:
homepage --> place the code in CMS -> pages -> home
success --> edit the success.phtml
cart --> edit the cart.phtml
categories -> create a CMS block --> place layout update in the concerning category page to load CMS block
But it looks like it is just a mess because for every code I have another solution. What would you suggest to get it done? Did I miss a better solution?
Thank you,
Hannes
If it's the same tracking code for each one, you could
Create a .phtml file containing the code
Add the file to the relevant hard pages (success, cart) using your layout xml files
Add the file to the relevant soft pages (home, categories) using the custom design tab / custom layout update area.
If the code changes based on the page, then you may need to create a new module with a block that alters the tracking code according to a parameter you include in the layout.
You can include a tracking script using the available Magento configurations fields.
Go to System > Configuration > Design
Under the HTML Head section, insert your tracking script into the Miscellaneous Scripts field.
Save Config, and clear your Magento caches.
Alternatively if you want it in the Footer instead, look under the Footer section and use the Miscellaneous HTML box there.

Create custom module, with javascript and load javascript into footer

I have been assigned to create joomla template from HTML template, and this is first time for me. In HTML template I have a custom JS, and as far I can tell, I will need to create one, or maybe two custom modules in order to recreate functionality that exists. Is there some way to write JS for specific module and load it in footer (this module will be active only on one page, so I don't want to load JS on all pages). Is it possible to do this, or the simplest way is to load JS on everypage?
Please read the Joomla documentation on creating modules Creating a simple module and then on your module file mod_mymodule.php add the script call using:
$doc = JFactory::getDocument();
$doc->addScript( JUri::root() . "media/your_js_directory/your_script.js" );
Hope it helps.

PyroCMS Custom Module Layout

I created a custom module called Drawings. I wanted to give it its own theme layout on the frontend, so I followed the advice I found HERE - I put a layout file titled Drawings.html in my theme.
This had the correct effect on the frontend, but caused other problems and questions that I've been struggling to sort out:
The backend admin section for my module now uses the Drawings.html layout, which breaks its functionality completely. How can I set a specific layout for my module front end, but keep the standard admin backend layout (same backend implementation as in the sample module tutorial)?
I read the following in the Template Library: "When using Public_Controller and Admin_Controller, the layout is already set." Perhaps the solution involves using the set_layout function in these controllers somehow? All my attempts failed so far though.
I feel I am doing something incorrectly because now my module is not modular. For example, after creating the Drawings.html layout file in the theme, a statement like {{ theme:css file="drawings.css"}} in that layout file searches for those resources in my theme. So I have to put those JS and CSS resources in my theme, instead of in my module's CSS and JS folders. How do I keep and access my resources in the module's JS and CSS folders?
I don't know how this is supposed to work.
When you are using front end controller use a layout there using philsturgeon template library like this
function index(){
$this->template->set_layout('drawing')
->build('yourview');
}
This way you can load a custom layout for your application.

In a custom Joomla! template, how can I determine whether I am on the home page?

I am trying to create a custom template, but I want the template to look a little different for my Joomla! homepage/frontpage. Is there a easy boolean-type expression I can use to determine whether I am on my site's homepage?
That way, I can just say if ($on_homepage) { [custom homepage html] } else { [regular template] }
Thanks in advance!
You can create a separate template for your homepage. More details here.
This is very easy, just create 2 templates. One for your homepage and one for the rest of your site. Then install both, make sure you give them a descriptive name in their xml file so you know the difference.
Once they are installed you can set a default template that would be site wide. Then choose/click on your homepage template and it's options will load up. On the bottom left you can select the menu items where this template will be visible. This setup is very much like the way in which you define where modules are shown.

Resources