I'm using couchdb\couchapp to host a web application.
I comes from Django, and with jinja2 I'm able to extend templates in two ways:
{% include "header.html" %}
or
{% extends "base.html" %} <<<---- preferred
I'm looking for a way to do the same with CouchDB, now I have header and footer code written in every single page and, obv, it doesn't look as best practice.
Thank you in advance.
Couch db supports common js modules which means that you can export the mustache/or other templating library as a string and then require it in your show function. More explanation on this mail archive
If you want to use JavaScript on the server side, you'll need to store it as a
property on your design doc. So in a "lib" folder (outside of _attachments)
with the 'couchapp' it will get included like:
couchapp folder
_id file
|_ _attachments folder
|_ ...clientside media...
|_ lib folder
|_ mustache.js
<---->
{_id:"", _attachments: {...}, lib:{mustache:""}}
Then you would use it in a _show/_list/_update function with var Mustache =
require('lib/mustache'). When I do need a library both server and clientside
with 'couchapp', I tend to symlink it so it appears in both _attachments and
other properties.
hope this helps,
-natevw
Related
How to set up/embed github profile or github repository cards on your Jekyll personal website?
I am expecting to embed my personal projects from github in the form of widgets on my personal website.
I don't see a direct way to include data in static Jekyll HTML files.
But there are options:
GitHub offers a JSON API for repos and users. Not sure what profile information you're looking for.
Jekyll has data files in YAML where you can store this data and read information from.
My initial idea is to get data in JSON and convert it to YAML, manually or programmatically. Then, you can build a Jekyll site that reads the information from the YAML files.
You can try out the manual way:
Read data from https://api.github.com/users/cadamini/repos using https://reqbin.com/
Convert the resulting JSON file using https://www.json2yaml.com/
Put the resulting YAML file in your Jekyll instance, e.g in _data/github/repos.yml (exclude the hyphens).
Extract:
- id: 540417594
node_id: R_kgDOIDYeOg
name: add-theme-github-pages-demo
full_name: cadamini/add-theme-github-pages-demo
private: false
Use the following code/syntax to access the information:
{% for item in data.github.repos %}
{{ item.name }}
{% endfor %}
Suppose I have assets/data/geo/regions.json file in my NUXT.js project folders structure. How can I read data from this file into my project?
I have tried axios but I don't know what URL will have this file, I have tried all possible URLs. What is the better solution to do that? Maybe better to hold JSON files in static folder?
Thanks!
If the regions.json file won't change, you can easily put it in the static folder.
Then the url will be /data/geo/regions.json
See this question on the nuxt issues page
You can import JSON files with import data from 'data.json' and use the data property straight in your component.
You may want to use "require" instead of "import" if you planning to load data within the loop.
jsons = ["json_one","json_two"]
jsons_readed = []
// In the loop
file = require(`./assets/data/geo/${jsons[i]}`)
jsons_readed.push(file)
Then I think you can use jsons_readed to access objects.
You can use Nuxt Content for that:
Empower your NuxtJS application with #nuxt/content module: write in a content/ directory and fetch your Markdown, JSON, YAML, XML and CSV files through a MongoDB like API, acting as a Git-based Headless CMS.
The basics are as easy as the following line. That will load the regions.json file, parse it, and store its content in the content variable. See Nuxt Content's documentation for more information about it.
const content = await this.$content('regions').fetch()
Alternatively you can read our blog post about
Using Nuxt Content with a JSON File. It describes how to extend existing pages with JSON content but also how to dynamically generate pages based on it.
Disclaimer: I work at FrontAid CMS.
My project is separated into many modules.
Each module has its own /templates folder that contains its own relevant template files.
There are common components (header, nav) inside these template files that I wish to extract into 'partials' which are then included inside the each module's template files (ie: {% include 'nav.html' %}
The Question
How can I set up my template environment in such a way that allows for the module's template files to reach into a shared folder that contains these partials?
My Current Environment Setup
template_dir = os.path.join(os.path.dirname(__file__), 'views')
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_dir),
autoescape=True)
self.render('some_template.html', env=env) # render function
This current configuration only allows me to reach into the /templates folder within each module. However, the templates files (inside the /templates dir) should also have access to the partial files that are outside of each module's directory.
Ideally, the app directory would look like this:
app/
shared/
header.html <-- templates within modules need to access these
nav.html
modules/
module_a/
handler.py
templates/
my_template.html <-- needs access to partials from 'shared/'
module_b/
module_c/
I'm trying to do this because I'm using a CMS where users will be able to edit a data file to make changes to the page instead of the _config.yml.
I'm wondering if it's possible to reference a variable from the data file and place this reference within the _config.yml.
Here's an example of what I'm trying to do;
Data File (/_data/site-data.yml)
navigation:
navigation_colour: '#462634'
Config File (/_config.yml)
defaults:
-
values:
navigation:
navigation_colour: site.data.site-data.navigation.navigation-colour
Is something similar to this possible?
Thanks!
You can assign at least in one config-file variables, I have not tested this over multiple files.
Variables inside YAML
YAML, hello will become Greetings earthling!
something: &hello Greetings earthling!
myref: *hello
MARKDOWN
{{ site.data.samplelist.myref }}
Jekyll does not parse variables in _config.yml. However inside your blog you can use liquid tags like {{site-data.navigation.navigation-colour}}. See here.
If its mandate to replace variables in _config.yml then use a custom or standard replacement plugin with grunt. So effective grunt build task will first perform token replacement in _config.yml and then do jekyll build.
This is my first time working with Django and while I'm finding the tutorial they provide to be very helpful, there is one major issue I'm having moving forward with my project.
The most confusing aspect of Django so far is the layout of files in a project. As of now, the layout of my project is as follows:
webapp/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
app/
__init__.py
models.py
tests.py
views.py
Bear with my naming here, I created a Django project "mysite" and an app "app". Here are the questions I find myself continually returning to:
I've noticed that in mysite/settings.py there is a section for apps, would I include the app I'm writing in this project in that section once I've finished it?
If I were to want to create a simple index.html page, where would a file like that go in this project organization?
I've read that static content like CSS or image files need to be contained within a simple "static" directory. Where would this go in this project organization? [ This would be for debugging purposes only, I've read this should never be done for production ]
My main goal right now is to just be able to view a simple html site before I begin delving into the models and views of the app I'm creating.
If your app needs one or more setting variables, then yes, you would put those in mysite/settings.py
Create a new folder mysite/templates/. This is where you want to put your template files. Organize your templates per app: mysite/templates/app/ would have the templates used by your app views. If you want to serve static templates such as a simple static index.html, then just throw that file in mysite/templates/index.html and then add the following to your urls.py file (no need to create a view for the index):
(r'^$', 'django.views.generic.simple.direct_to_template', {'template': 'index.html'}),
Static content would end up in something like mysite/static after you run the collectstatic command. See managing static files. The collectstatic command searches for static files in all locations specified in STATICFILES_DIRS and deploys them into the folder specified in STATIC_ROOT (mysite/static).