Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use expressions plugin with it #7

Open
cossssmin opened this issue May 2, 2020 · 3 comments
Open

Can't use expressions plugin with it #7

cossssmin opened this issue May 2, 2020 · 3 comments

Comments

@cossssmin
Copy link
Member

So looking at plugins.before and plugins.after, I understand you can pass an array of PostHTML plugins to run before and after the remote content is fetched.

I'm trying to pass in posthtml-expressions, because I already have some variables in context, that I need to use inside the <fetch> tag:

const fetch = require('posthtml-fetch')

module.exports = (html, config) => {
  // config is an object I already have
  const fetchPlugin = fetch({
    plugins: {
      before: [ expressions({locals: {page: config}}) ]
    }
  })
  
  return posthtml([fetchPlugin]).process(html).then(result => result.html)
}

At this point, I would expect both the response variable from posthtml-fetch and the page variable that I'm passing in plugins.before with expressions to be available.

I'll use a real example so you can reproduce it - imagine I already had page.title in context:

<fetch url="https://css-tricks.com/wp-json/wp/v2/posts?page=1&per_page=7&offset=1&_embed=1">
  {{ page.title }}
  {{ response[0].title.rendered }}
</fetch>

This results in a ReferenceError: page is not defined

One way around it is if posthtml-fetch would accept a locals option.

However, I think passing in expressions with its own locals, before the content is fetched, should work.

Am I missing something here?

@Scrum
Copy link
Member

Scrum commented May 25, 2020

Hi, I got to this task #7, I found some problems that were fixed, but the question remains about response by a call

before: [ expressions({locals: {page: config}}) ]

this value is not currently available and how would you like it to work ?

@cossssmin
Copy link
Member Author

Imagine I already have an object with data that I want to make available to the response after it is fetched. So I may already have a variable, let's say language which equals en.

With that, I should be able to do:

<fetch url="...">
  {{ language }}
  <!-- now render response -->
  {{ response.title }}
</fetch>

... and get:

en
<!-- now render response -->
response title text

If I do this now, I get an error because language is not defined when used inside the <fetch> tag, even though 'globally' it's available (i.e. outside the fetch tag).

Makes sense?

@Scrum
Copy link
Member

Scrum commented May 28, 2020

I so believe that we need a mechanism that would tell expression what to process and what to process then so ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants