Pass Parameters to Umbraco Macro From Static Content Page
Hi All,
Can we pass parameters to Macro from static content pages? If yes please let me know the way to do it.
As per my information we can place macro in the Rich Text Editor from one of the options available in the toolbar. I am not sure how to pass parameter to such an embedded macro.
As per the solution provided by you here we are inserting macro in a template.
But suppose we have a different scenario in which we have 15 content pages which are implementing the same template but out of these 15 content pages we need a macro and need to pass parameters to the macro in only 2 of these content pages.
In that scenario there is no need to handle macro in the template but rather we should be handling macro in those 2 content pages.
But we are not sure how to pass parameters to macros through the content pages.
Thanks for an elaborative answer. We are now able to pass the parameter from the static content page.
But suppose as per my previous example of using macro in only 2 of static pages we won’t include our macro in template but will rather add macro only in the static content page using Rich Text Box editor. In that scenario while adding macro it asks for the parameter value and when we write @Model.LastName in parameter value text box our macro doesn’t work, and if pass some hard coded text value our macro works.
Just for your information we have model passed into our template as @inherits Umbraco.Web.Mvc.UmbracoViewPage
I tried it as @CurrentPage.LastName but it gives a blank value. I further debugged it and even if write any name after @CurrentPage, e.g. @CurrentPage.ABCDEF it gives me blank message.
Just for your information we have model passed into our template as @inherits Umbraco.Web.Mvc.UmbracoViewPage .
Model is accessible on the temple as @Model.LastName but same code if i write i.e. @Model.LastName in macro it gives an error.
Pass Parameters to Umbraco Macro From Static Content Page
Hi All,
Can we pass parameters to Macro from static content pages? If yes please let me know the way to do it.
As per my information we can place macro in the Rich Text Editor from one of the options available in the toolbar. I am not sure how to pass parameter to such an embedded macro.
Hi Tarunjit.
Yes, you can pass in the parameters as dictionary parameters.
Here is a very basic example:
The macro partial:
The macro parameters:
Passing the parameters and render the macro in view:
Here is a little more info regarding macros and managing parameters: https://our.umbraco.org/documentation/Reference/Templating/Macros/managing-macros (The images is Umbraco 6 but the principle is the same.)
Best of luck to you, let me know if you need anything else.
/ Dennis
Ps. You can also use the "Insert Macro" tool from the Template, see animation:
Hi Dennis,
Thanks for the answer.
As per the solution provided by you here we are inserting macro in a template.
But suppose we have a different scenario in which we have 15 content pages which are implementing the same template but out of these 15 content pages we need a macro and need to pass parameters to the macro in only 2 of these content pages.
In that scenario there is no need to handle macro in the template but rather we should be handling macro in those 2 content pages. But we are not sure how to pass parameters to macros through the content pages.
Please help me on this.
In that scenario, there is a datatype called Macro container, that you´d probobly are looking to use.
Create a new property for your content page documenttype, with the datatype "Macro Container" then add this to your Template. See animation:
This way, you only have to set this property for the 2 pages, and not the other 13 in your example.
Would that work for you?
Hi Dennis,
Thanks for an elaborative answer. We are now able to pass the parameter from the static content page.
But suppose as per my previous example of using macro in only 2 of static pages we won’t include our macro in template but will rather add macro only in the static content page using Rich Text Box editor. In that scenario while adding macro it asks for the parameter value and when we write @Model.LastName in parameter value text box our macro doesn’t work, and if pass some hard coded text value our macro works.
Just for your information we have model passed into our template as @inherits Umbraco.Web.Mvc.UmbracoViewPage
Please help me on this.
In that scenario, i would skip having lastname as a parameter, and access the current content page directly from within the macro like this:
Hi Dennis,
Thanks for the answer.
I tried it as @CurrentPage.LastName but it gives a blank value. I further debugged it and even if write any name after @CurrentPage, e.g. @CurrentPage.ABCDEF it gives me blank message.
Just for your information we have model passed into our template as @inherits Umbraco.Web.Mvc.UmbracoViewPage .
Model is accessible on the temple as @Model.LastName but same code if i write i.e. @Model.LastName in macro it gives an error.
Please help me on this as i am struck here.
is working on a reply...