Magento - create a module with simple .phtml template - magento

I want to create a module with simple .phtml template, that have to be included in the page "cart.phtml" and also in "noItems.phtml"
How can do that ?
Thanks a lot.

You can create a simple module as shown in this >> tutorial.. and once you finished the weblog example in the above link, you can take a look at this link >> to create and place a phtml file.
Balan

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"}}

How i can create url from .phtml in magento

I want in magento create link as it. From .phtml file. I am can't find method for did it
magento/index.php/quotes/index/create/id/1/
Try this:
Mage::getUrl('quotes/index/create', array('id'=>1));

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.

Place static javascript code on all pages

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.

Block Method Calls in CMS Page

Is there any a way to call Block method calls in CMS page?
What I am trying to do is. I have a hyperlink in my CMS Page and I want to retrieve the email address of the customer to pass it as a get variable which another website will use.
e.g. In CMS page I have
(someother website link)
<a href="www.xyz.com?email=<?php $getCutomer->getEmail();?>&&name=<?php $getCutomer->getName();?>
I know we can't add php in CMS pages or block. I have just shown you as an example what I want to achieve.
So is there any way using XML or anything else?
Please Advise. Thanks
Yes as MagePsycho said, you will need to create phtml file for calling block method or getting dynamic data.
In .phtml file, it's simple just call $this->getCustomerEmail() or whatever method you wrote to get customer email.
Why can't you simply include the .phtml file in CMS page or Static block as:
{{block type="core/template" template="path/to/your-custom-template.phtml"}}
and you can now use any php code in that .phtml file.
Cheers!

Resources