1

I have this string in JSON format:

"message": "<?= __('I agree to the <a>Terms of Service</a>', $block->getUrl('terms-conditions')) ?>"

What I want to do is add a link to the string of this form

 "message": "<?= __('I agree to the <a href="%1">Terms of Service</a>', $block->getUrl('terms-conditions')) ?>"

But when I do this I get the following error:

SyntaxError: Unexpected token h in JSON

How can I add the link to the tag in a correct way?

asked Apr 24, 2019 at 10:05
2
  • Don't use a template to generate JSON. Build your data structure and then run it through JSON.stringify. Commented Apr 24, 2019 at 10:07
  • Escape quotes in href. \"%1\" Commented Apr 24, 2019 at 10:08

2 Answers 2

3

You have to escape the double quotes in the string like:

 "message": "<?= __('I agree to the <a href=\"%1\">Terms of Service</a>', $block->getUrl('terms-conditions')) ?>"
answered Apr 24, 2019 at 10:08
Sign up to request clarification or add additional context in comments.

Comments

2

If you have any string, it's best to just convert it to json, the function will handle all your characters that require escaping.

In php it can look like this:

"message": <?= json_encode(__('I agree to the <a>Terms of Service</a>', $block->getUrl('terms-conditions'))) ?>

each language should have similar json encode function, just search in google for you language.

answered Apr 24, 2019 at 10:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.