How to get values passed from phtml in js magento 2?How to pass phtml variable in js file in magento2Magento 2: can't use materialize.js on my custom moduleHow to hide magento 2.2 messages after a time has passed?Magento 2 - Trying to get the items from the order into the success pageHow to get the shipping address from using sales_order_payment_save_after eventDeleting values from `_cl` tablesMagento 2.2.6 VAT not passed to paypalHow to pass variable from phtml and how to get variable in require js fileHow to create a default Magento 2 block (catalog/product_list) from a phtml file?Need to get array valuesHow to pass variable value from phtml JS to block.php?
3D Statue Park: U shapes
Why are off grid solar setups only 12, 24, 48 VDC?
Can two figures have the same area, perimeter, and same number of segments have different shape?
Why are so many countries still in the Commonwealth?
How do we explain the E major chord in this progression?
Why are there not any MRI machines available in Interstellar?
How to deal with a player who makes bad characters and kills them?
Why was Sauron preparing for war instead of trying to find the ring?
TSA asking to see cell phone
Send a single HTML email from Thunderbird, overriding the default "plain text" setting
What to do when you reach a conclusion and find out later on that someone else already did?
Is my employer paying me fairly? Going from 1099 to W2
How can I prevent corporations from growing their own workforce?
What do teaching faculty do during semester breaks?
High income, sudden windfall
Why is drive/partition number still used?
Why isn't there a ";" after "do" in sh loops?
(1 of 11: Numberlink) What is Pyramid Cult's Favorite Activity?
Drillers for petroleum strike gusher of blood
Basic Questions on Wiener Filtering
What do I do when a student working in my lab "ghosts" me?
Keeping an "hot eyeball planet" wet
Is it normal practice to screen share with a client?
How can I create a pattern of parallel lines that are increasing in distance in Photoshop / Illustrator?
How to get values passed from phtml in js magento 2?
How to pass phtml variable in js file in magento2Magento 2: can't use materialize.js on my custom moduleHow to hide magento 2.2 messages after a time has passed?Magento 2 - Trying to get the items from the order into the success pageHow to get the shipping address from using sales_order_payment_save_after eventDeleting values from `_cl` tablesMagento 2.2.6 VAT not passed to paypalHow to pass variable from phtml and how to get variable in require js fileHow to create a default Magento 2 block (catalog/product_list) from a phtml file?Need to get array valuesHow to pass variable value from phtml JS to block.php?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have passed two variables in my phtml to js file as below
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?php /* @escapeNotVerified */ echo $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?php /* @escapeNotVerified */ echo $block->getConfigData('autoplay_speed'); ?>
</script>
My js file is
require([
'jquery',
'slick'
], function ($)
$(document).ready(function ()
var slickConfig =
autoplay: true,
infinite: true,
arrows: true,
dots: true,
draggable: false,
slidesToShow: 1,
slidesToScroll: 1,
autoplaySpeed: 1000,
;
$('.slider-gallery').slick(slickConfig);
);
);
How to get values of these variables in my js file?
javascript magento2.2.6
|
show 2 more comments
I have passed two variables in my phtml to js file as below
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?php /* @escapeNotVerified */ echo $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?php /* @escapeNotVerified */ echo $block->getConfigData('autoplay_speed'); ?>
</script>
My js file is
require([
'jquery',
'slick'
], function ($)
$(document).ready(function ()
var slickConfig =
autoplay: true,
infinite: true,
arrows: true,
dots: true,
draggable: false,
slidesToShow: 1,
slidesToScroll: 1,
autoplaySpeed: 1000,
;
$('.slider-gallery').slick(slickConfig);
);
);
How to get values of these variables in my js file?
javascript magento2.2.6
get reference from here
– Hitesh
Oct 9 '18 at 4:46
Can you please post getConfigData() function code here?
– Rohan Hapani
Oct 9 '18 at 5:11
public function getConfigData($fieldName) $configValue = $this->_scopeConfig->getValue( 'slider/slider_js_config/' . $fieldName, MagentoStoreModelScopeInterface::SCOPE_STORE); return $this->_jsonHelper->jsonEncode($configValue);
– Magento User
Oct 9 '18 at 5:16
Please check my updated answer
– Rohan Hapani
Oct 9 '18 at 5:21
Is it working for you?
– Rohan Hapani
Oct 9 '18 at 5:31
|
show 2 more comments
I have passed two variables in my phtml to js file as below
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?php /* @escapeNotVerified */ echo $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?php /* @escapeNotVerified */ echo $block->getConfigData('autoplay_speed'); ?>
</script>
My js file is
require([
'jquery',
'slick'
], function ($)
$(document).ready(function ()
var slickConfig =
autoplay: true,
infinite: true,
arrows: true,
dots: true,
draggable: false,
slidesToShow: 1,
slidesToScroll: 1,
autoplaySpeed: 1000,
;
$('.slider-gallery').slick(slickConfig);
);
);
How to get values of these variables in my js file?
javascript magento2.2.6
I have passed two variables in my phtml to js file as below
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?php /* @escapeNotVerified */ echo $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?php /* @escapeNotVerified */ echo $block->getConfigData('autoplay_speed'); ?>
</script>
My js file is
require([
'jquery',
'slick'
], function ($)
$(document).ready(function ()
var slickConfig =
autoplay: true,
infinite: true,
arrows: true,
dots: true,
draggable: false,
slidesToShow: 1,
slidesToScroll: 1,
autoplaySpeed: 1000,
;
$('.slider-gallery').slick(slickConfig);
);
);
How to get values of these variables in my js file?
javascript magento2.2.6
javascript magento2.2.6
asked Oct 9 '18 at 4:43
Magento UserMagento User
2731 silver badge13 bronze badges
2731 silver badge13 bronze badges
get reference from here
– Hitesh
Oct 9 '18 at 4:46
Can you please post getConfigData() function code here?
– Rohan Hapani
Oct 9 '18 at 5:11
public function getConfigData($fieldName) $configValue = $this->_scopeConfig->getValue( 'slider/slider_js_config/' . $fieldName, MagentoStoreModelScopeInterface::SCOPE_STORE); return $this->_jsonHelper->jsonEncode($configValue);
– Magento User
Oct 9 '18 at 5:16
Please check my updated answer
– Rohan Hapani
Oct 9 '18 at 5:21
Is it working for you?
– Rohan Hapani
Oct 9 '18 at 5:31
|
show 2 more comments
get reference from here
– Hitesh
Oct 9 '18 at 4:46
Can you please post getConfigData() function code here?
– Rohan Hapani
Oct 9 '18 at 5:11
public function getConfigData($fieldName) $configValue = $this->_scopeConfig->getValue( 'slider/slider_js_config/' . $fieldName, MagentoStoreModelScopeInterface::SCOPE_STORE); return $this->_jsonHelper->jsonEncode($configValue);
– Magento User
Oct 9 '18 at 5:16
Please check my updated answer
– Rohan Hapani
Oct 9 '18 at 5:21
Is it working for you?
– Rohan Hapani
Oct 9 '18 at 5:31
get reference from here
– Hitesh
Oct 9 '18 at 4:46
get reference from here
– Hitesh
Oct 9 '18 at 4:46
Can you please post getConfigData() function code here?
– Rohan Hapani
Oct 9 '18 at 5:11
Can you please post getConfigData() function code here?
– Rohan Hapani
Oct 9 '18 at 5:11
public function getConfigData($fieldName) $configValue = $this->_scopeConfig->getValue( 'slider/slider_js_config/' . $fieldName, MagentoStoreModelScopeInterface::SCOPE_STORE); return $this->_jsonHelper->jsonEncode($configValue);
– Magento User
Oct 9 '18 at 5:16
public function getConfigData($fieldName) $configValue = $this->_scopeConfig->getValue( 'slider/slider_js_config/' . $fieldName, MagentoStoreModelScopeInterface::SCOPE_STORE); return $this->_jsonHelper->jsonEncode($configValue);
– Magento User
Oct 9 '18 at 5:16
Please check my updated answer
– Rohan Hapani
Oct 9 '18 at 5:21
Please check my updated answer
– Rohan Hapani
Oct 9 '18 at 5:21
Is it working for you?
– Rohan Hapani
Oct 9 '18 at 5:31
Is it working for you?
– Rohan Hapani
Oct 9 '18 at 5:31
|
show 2 more comments
3 Answers
3
active
oldest
votes
This is a simple example of passing data from phtml template to your js using js-component.
app/code/Anshu/Custom/view/frontend/requirejs-config.js
var config =
map:
'*':
myjscomponent: 'Anshu_Custom/js/customjs'
;
app/code/Anshu/Custom/view/frontend/templates/myTemplate.phtml
<?php
$yourCustomValue = 'My Custom Data';
?>
<script type="text/x-magento-init">
"*":
"myjscomponent":
"customData": <?= $yourCustomValue ?>
</script>
app/code/Anshu/Custom/view/frontend/web/js/customjs.js
define([], function()
'use strict';
return function(config, element)
var myData = config.customData;
console.log(myData);
);
You can modify the code according to your requirement.
add a comment |
Try to use this below code in your js :
Modify this below code in your phtml file :
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay_speed'); ?>
</script>
Now, you can get value using this below way :
console.log(this.autoplay);
console.log(this.autoplay_speed);
No. It is showing undefined...
– Magento User
Oct 9 '18 at 5:10
add a comment |
In your .phtml file,
....
<?php $_helper = $this->helper('NamespaceModuleHelperData');
$zoomType = $_helper->getZoomType(); ?>
.....
<script type="text/x-magento-init">
"*":
"quickView":
"zoomType": "<?= __($zoomType) ?>"
</script>
And Obtain zoomType is your JS like,
define([
'jquery',
'underscore',
'Magento_Ui/js/modal/modal'
], function ($, _, modal)
"use strict";
return function (config)
console.log(config.zoomType); //Checked passed variable here
if (config.zoomType == 'hover')
$(document).ready(function()
$('.fotorama__stage').live('mouseleave', function()
$('.magnify-lens').addClass('magnify-hidden');
$('.magnifier-preview').addClass('magnify-hidden');
);
);
$(document).ready(function()
var documentPadding = 25;
var firstAttempt = true;
................
................
................
);
;
);
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f245640%2fhow-to-get-values-passed-from-phtml-in-js-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a simple example of passing data from phtml template to your js using js-component.
app/code/Anshu/Custom/view/frontend/requirejs-config.js
var config =
map:
'*':
myjscomponent: 'Anshu_Custom/js/customjs'
;
app/code/Anshu/Custom/view/frontend/templates/myTemplate.phtml
<?php
$yourCustomValue = 'My Custom Data';
?>
<script type="text/x-magento-init">
"*":
"myjscomponent":
"customData": <?= $yourCustomValue ?>
</script>
app/code/Anshu/Custom/view/frontend/web/js/customjs.js
define([], function()
'use strict';
return function(config, element)
var myData = config.customData;
console.log(myData);
);
You can modify the code according to your requirement.
add a comment |
This is a simple example of passing data from phtml template to your js using js-component.
app/code/Anshu/Custom/view/frontend/requirejs-config.js
var config =
map:
'*':
myjscomponent: 'Anshu_Custom/js/customjs'
;
app/code/Anshu/Custom/view/frontend/templates/myTemplate.phtml
<?php
$yourCustomValue = 'My Custom Data';
?>
<script type="text/x-magento-init">
"*":
"myjscomponent":
"customData": <?= $yourCustomValue ?>
</script>
app/code/Anshu/Custom/view/frontend/web/js/customjs.js
define([], function()
'use strict';
return function(config, element)
var myData = config.customData;
console.log(myData);
);
You can modify the code according to your requirement.
add a comment |
This is a simple example of passing data from phtml template to your js using js-component.
app/code/Anshu/Custom/view/frontend/requirejs-config.js
var config =
map:
'*':
myjscomponent: 'Anshu_Custom/js/customjs'
;
app/code/Anshu/Custom/view/frontend/templates/myTemplate.phtml
<?php
$yourCustomValue = 'My Custom Data';
?>
<script type="text/x-magento-init">
"*":
"myjscomponent":
"customData": <?= $yourCustomValue ?>
</script>
app/code/Anshu/Custom/view/frontend/web/js/customjs.js
define([], function()
'use strict';
return function(config, element)
var myData = config.customData;
console.log(myData);
);
You can modify the code according to your requirement.
This is a simple example of passing data from phtml template to your js using js-component.
app/code/Anshu/Custom/view/frontend/requirejs-config.js
var config =
map:
'*':
myjscomponent: 'Anshu_Custom/js/customjs'
;
app/code/Anshu/Custom/view/frontend/templates/myTemplate.phtml
<?php
$yourCustomValue = 'My Custom Data';
?>
<script type="text/x-magento-init">
"*":
"myjscomponent":
"customData": <?= $yourCustomValue ?>
</script>
app/code/Anshu/Custom/view/frontend/web/js/customjs.js
define([], function()
'use strict';
return function(config, element)
var myData = config.customData;
console.log(myData);
);
You can modify the code according to your requirement.
answered Oct 9 '18 at 5:14
Anshu MishraAnshu Mishra
5,9265 gold badges28 silver badges65 bronze badges
5,9265 gold badges28 silver badges65 bronze badges
add a comment |
add a comment |
Try to use this below code in your js :
Modify this below code in your phtml file :
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay_speed'); ?>
</script>
Now, you can get value using this below way :
console.log(this.autoplay);
console.log(this.autoplay_speed);
No. It is showing undefined...
– Magento User
Oct 9 '18 at 5:10
add a comment |
Try to use this below code in your js :
Modify this below code in your phtml file :
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay_speed'); ?>
</script>
Now, you can get value using this below way :
console.log(this.autoplay);
console.log(this.autoplay_speed);
No. It is showing undefined...
– Magento User
Oct 9 '18 at 5:10
add a comment |
Try to use this below code in your js :
Modify this below code in your phtml file :
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay_speed'); ?>
</script>
Now, you can get value using this below way :
console.log(this.autoplay);
console.log(this.autoplay_speed);
Try to use this below code in your js :
Modify this below code in your phtml file :
<script type="text/x-magento-init">
".slider-gallery":
"slickSlider":
"autoplay": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay'); ?>,
"autoplay_speed": <?= /* @escapeNotVerified */ $block->getConfigData('autoplay_speed'); ?>
</script>
Now, you can get value using this below way :
console.log(this.autoplay);
console.log(this.autoplay_speed);
edited Oct 9 '18 at 5:20
answered Oct 9 '18 at 5:08
Rohan HapaniRohan Hapani
8,0964 gold badges21 silver badges66 bronze badges
8,0964 gold badges21 silver badges66 bronze badges
No. It is showing undefined...
– Magento User
Oct 9 '18 at 5:10
add a comment |
No. It is showing undefined...
– Magento User
Oct 9 '18 at 5:10
No. It is showing undefined...
– Magento User
Oct 9 '18 at 5:10
No. It is showing undefined...
– Magento User
Oct 9 '18 at 5:10
add a comment |
In your .phtml file,
....
<?php $_helper = $this->helper('NamespaceModuleHelperData');
$zoomType = $_helper->getZoomType(); ?>
.....
<script type="text/x-magento-init">
"*":
"quickView":
"zoomType": "<?= __($zoomType) ?>"
</script>
And Obtain zoomType is your JS like,
define([
'jquery',
'underscore',
'Magento_Ui/js/modal/modal'
], function ($, _, modal)
"use strict";
return function (config)
console.log(config.zoomType); //Checked passed variable here
if (config.zoomType == 'hover')
$(document).ready(function()
$('.fotorama__stage').live('mouseleave', function()
$('.magnify-lens').addClass('magnify-hidden');
$('.magnifier-preview').addClass('magnify-hidden');
);
);
$(document).ready(function()
var documentPadding = 25;
var firstAttempt = true;
................
................
................
);
;
);
add a comment |
In your .phtml file,
....
<?php $_helper = $this->helper('NamespaceModuleHelperData');
$zoomType = $_helper->getZoomType(); ?>
.....
<script type="text/x-magento-init">
"*":
"quickView":
"zoomType": "<?= __($zoomType) ?>"
</script>
And Obtain zoomType is your JS like,
define([
'jquery',
'underscore',
'Magento_Ui/js/modal/modal'
], function ($, _, modal)
"use strict";
return function (config)
console.log(config.zoomType); //Checked passed variable here
if (config.zoomType == 'hover')
$(document).ready(function()
$('.fotorama__stage').live('mouseleave', function()
$('.magnify-lens').addClass('magnify-hidden');
$('.magnifier-preview').addClass('magnify-hidden');
);
);
$(document).ready(function()
var documentPadding = 25;
var firstAttempt = true;
................
................
................
);
;
);
add a comment |
In your .phtml file,
....
<?php $_helper = $this->helper('NamespaceModuleHelperData');
$zoomType = $_helper->getZoomType(); ?>
.....
<script type="text/x-magento-init">
"*":
"quickView":
"zoomType": "<?= __($zoomType) ?>"
</script>
And Obtain zoomType is your JS like,
define([
'jquery',
'underscore',
'Magento_Ui/js/modal/modal'
], function ($, _, modal)
"use strict";
return function (config)
console.log(config.zoomType); //Checked passed variable here
if (config.zoomType == 'hover')
$(document).ready(function()
$('.fotorama__stage').live('mouseleave', function()
$('.magnify-lens').addClass('magnify-hidden');
$('.magnifier-preview').addClass('magnify-hidden');
);
);
$(document).ready(function()
var documentPadding = 25;
var firstAttempt = true;
................
................
................
);
;
);
In your .phtml file,
....
<?php $_helper = $this->helper('NamespaceModuleHelperData');
$zoomType = $_helper->getZoomType(); ?>
.....
<script type="text/x-magento-init">
"*":
"quickView":
"zoomType": "<?= __($zoomType) ?>"
</script>
And Obtain zoomType is your JS like,
define([
'jquery',
'underscore',
'Magento_Ui/js/modal/modal'
], function ($, _, modal)
"use strict";
return function (config)
console.log(config.zoomType); //Checked passed variable here
if (config.zoomType == 'hover')
$(document).ready(function()
$('.fotorama__stage').live('mouseleave', function()
$('.magnify-lens').addClass('magnify-hidden');
$('.magnifier-preview').addClass('magnify-hidden');
);
);
$(document).ready(function()
var documentPadding = 25;
var firstAttempt = true;
................
................
................
);
;
);
edited Jul 18 at 12:07
answered Jul 17 at 10:12
Kazim NooraniKazim Noorani
1,2241 gold badge8 silver badges24 bronze badges
1,2241 gold badge8 silver badges24 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f245640%2fhow-to-get-values-passed-from-phtml-in-js-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
get reference from here
– Hitesh
Oct 9 '18 at 4:46
Can you please post getConfigData() function code here?
– Rohan Hapani
Oct 9 '18 at 5:11
public function getConfigData($fieldName) $configValue = $this->_scopeConfig->getValue( 'slider/slider_js_config/' . $fieldName, MagentoStoreModelScopeInterface::SCOPE_STORE); return $this->_jsonHelper->jsonEncode($configValue);
– Magento User
Oct 9 '18 at 5:16
Please check my updated answer
– Rohan Hapani
Oct 9 '18 at 5:21
Is it working for you?
– Rohan Hapani
Oct 9 '18 at 5:31