posthtml/posthtml-include

include locals don't merge with posthtml-expressions config locals

danielrob opened this issue · 10 comments

It would be great to be able to
.posthtmlrc

"posthtml-expressions": {
  "locals": {
    "href_prefix": "/"
  }
},

.index.html

<include src="site-header.html" locals='{"page": "index"}'></include>

And still have access to both page and href_prefix inside of the site-header.html include.

Currently, href_prefix will be overwritten and become undefined.

The only workaround I could find is to e.g. <include src="site-header.html" locals='{"href_prefix": "{{href_prefix}}","page": "index"}'></include> which is a little cumbersome.

Versions

    "posthtml-expressions": {
      "version": "1.4.1",
    },
    "posthtml-include": {
      "version": "1.4.3",
    }

@danielrob How did you fix this? the global defined still do not merge in the local defined ones....

@verhulstd I'd have to do a lot of digging... was a long time ago now. Sorry!

Have you found a way around it? I'm having the same issue with a project. I've tried posthtml-include and posthtml-modules.

Scrum commented

Have you found a way around it? I'm having the same issue with a project. I've tried posthtml-include and posthtml-modules.

Hi, I've already fallen out of context, could you create a repository to reproduce the problem?

Hi,

A.html

<include
  src="./src/partials/_head.html"
  locals='{
    "title": "Building Your Personal Brand",
    "image": "/src/assets/brand.jpg"
  }'
></include>

_head.html

<meta property="title" content="{{ title }}" />
<if condition="{{image}}">
  <meta property="og:image" content="{{ image }}" />
</if>
<else>
  <meta property="og:image" content=".src/assets/logo.jpg" />
</else>

The {{ image }} inside the tag is always undefined as it doesn't get passed down from the in the caller HTML.

@gbronca that looks like a different issue. That should work. The issue is when global variables from the .posthmlrc are defined, that those variables don't get passed in a case like you posted when other locals are provided.

@danielrob what I get from the variable in the block is undefined. If I move the meta property outside the if block it works fine.

That is my .posthtmlrc

{
  "plugins": {
    "posthtml-modules": { },
    "posthtml-include": { },
    "posthtml-expressions": { }
  }
}

For me, the key to success was to:

  1. Switch to posthtml-modules
  2. Pass the same locals to both posthtml-modules and posthtml-expressions:
{
  "plugins": {
    "posthtml-modules": {
	  "locals": {
		"foo": 1
	  }
	},
    "posthtml-expressions": {
	  "locals": {
		"foo": 1
	  }
	}
  }
}

In my case I have a postcss.config.js so I could just move locals to a variable.

Scrum commented

publish v1.7.2