magento 2 custom phtml page - magento

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

Related

How do I stop Magento from inserting <br> tags automatically in product descriptions?

Whenever you want to add some custom html to product descriptions in Magento you start getting br tags on the frontend that seem to come out of nowhere.
There is a Magento function that's causing this, in order to disable it go to either:
/app/design/frontend/base/default/template/catalog/product/view/description.phtml
OR
/app/design/frontend/base/default/template/catalog/product/view.phtml
The actual file to be modified depends on your version of Magento.
You should find a reference to nl2br in the correct file.
Copy this file in your custom template DO NOT MODIFY CORE FILES
(they will come back to bite you).
Remove 'nl2br'
Clear cache

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.

Magento Multi Store themes calling same login.phtml file

I'm new to magento. Currently i'm developing two ecommerce sites using multi-store option in magento. Both the sites are 90% over, last night when i planned to customize the login page i was shocked. The reason is the changes made in first store login page also reflecting in second store also. The login page is curently calling from
frontend/default/default/template/persistent/customer/form/login.phtml
Is it possible to give any custom login page? PLz guide me magento experts......
Please update your post with your frontend/ folder structure down to each theme template folder. It might be that you don't have a copy of login.phtml in the second theme. Moreover you should follow templates best practices as:
Always create for a new shop different packaga. So not
frontend/default
but
frontend/yourcustompackage
Always have default theme left as much untouched, as possible. So in the
frontend/package/default
Should be some basic changes and rules of your package. It means for example in the putting reset.css for all of your themes. Or something like this.
As for me, each store view should have own theme. So, for example, storeview1
frontend/package/storeview1/
There you copy and change files specific for this theme. So, for example, in default you might have login.phtml. But you want storeview1 to have another login.phtml and in storeview2 it should be the same as in default. Therefore you copy the login.phtml from default to storeview1 and change it there. Don't forget to change package and theme in System->Configuration->Design. Package should be change for whole shop and theme should be specified for every store view or left untouched for default.
P.S. Magento theme fallbacks
P.P.S. Try this tutorial or google another one
P.P.P.S. I hope, you got the answer for your question.

Calling a Static Custom Module Block in a CMS page

In my CMS contact-us page, I have the following code:
{{block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"}}
Which pulls in Magento's standard Contact Form. I have installed a new module from Fontis called recaptcha to prevent spam. This is located my_theme/default/template/fontis/recaptcha/contacts.phtml.
Is anyone able to tell me how I correctly modify the above code to use the new form please?
I have tried changing: template="contacts/form.phtml" to template="fontis/recaptcha/contacts.phtml" but this doesnt seem to work, not sure if I need to alter the block type to make it work.
Thanks in advance for any help
John
Your modification is generally correct to change a template path. Specifically, what do you mean when you say it "doesn't" work? Without more information on what you're seeing, it's difficult to help you debug this issue.
Thanks!
Joe
Sounds like a caching issue to me, or that you're changing the template on the wrong declaration. First, turn off all caching on your site and try again. If that doesn't work, turn on template path hints and make sure that you are editing what you think you are.

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