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

Partials with params #31

Open
fidian opened this issue Jul 6, 2019 · 3 comments
Open

Partials with params #31

fidian opened this issue Jul 6, 2019 · 3 comments

Comments

@fidian
Copy link
Contributor

fidian commented Jul 6, 2019

Split from #28 by @sc0ttj:

Does the following (or something similar) work in mo? ..

{{>some_partial
  param1="one"
  param2=2
  param3="$some_shell_var"
  param4="{{some_var_or_func}}"
}}

...I have some snippets lying around which nearly make it work with params as strings, but it's not working yet, and I didn't actually test mo itself to see if the above, or something similar, is supported..

@fidian
Copy link
Contributor Author

fidian commented Jul 6, 2019

No, it does not. I don't have a tokenizer or a bash syntax parser that would allow this, especially for param4.

@fidian fidian mentioned this issue Jul 6, 2019
@fidian
Copy link
Contributor Author

fidian commented Jul 10, 2019

Leaving this one open because it's a wonderful idea.

@sc0ttj
Copy link

sc0ttj commented Oct 5, 2019

Leaving this here, as this is still very much something I am interested in implementing:

NOTE: The following would be good enough (slightly easier than original example):

{{>some_partial
  param1="one"
  param2="2"
  param3="$some_shell_var"
}}

Below is some code that might help... (Obviously this code needs fixing up, making faster, removing hard-coded dir stuff, but gives an idea of what I might be able to implement)

Suggestions, improvements and alternatives are welcome!

#!/bin/bash

# A shell function for `mo` - it adds support for mustache partials,
# with support for passing paramters to `mo`, for custom output.


# TDLR: this function replaces
#
#    {{>foo param1=bar param2=baz}}
#
# with
#
#    param1=bar param2=baz ${PARTIAL_DIR:-$PWD}/templates/_foo.mustache | mo
#
# It should be used inside `mo`, just before it parses the partial file - if params 
# given, run/eval the command generated by the `process_partial` func below 
# instead of the usual partial processing 

# If $PARTIAL_DIR is not set, the current working directory ($PWD) is where
# the function will look for partial files.


# Usage:
#
#    process_partial {{>page-header title="bar" desc="one" three="3"}}


process_partial() {
  local partial="$(echo "$@" | cut -f1 -d ' '   | sed 's/{{>//')"

  if [ ! -f ${PARTIAL_DIR:-$PWD}/templates/_${partial}.mustache ];then
    echo "Error: partial file '${partial}.mustache' not found in: "
    echo "${PARTIAL_DIR:-$PWD}/templates"
    echo
    return 1
  fi

  local params="$(echo  "$@" | cut -f2-  -d' ' | sed "s/}}$//")"

  source `which mo`

  ${params} cat ${PARTIAL_DIR:-$PWD}/templates/_${partial}.mustache | mo
}

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