-1

I'm doing statics on html5. I have many HTML lines of the same type:

<meta name="keywords" content="any keywords" />
<meta name="description" content="any description" />

The site has many pages, and editing these lines takes a lot of time for each individual file *.erb.

I wanted to find out how to call the required string from a single * .erb file. If I use <%= partial '...' %> in this case, the entire file will be called. Tell me if any opportunity to call only those lines which are necessary. I do not know how to call any line parts of a file from one file *.erb using middleman3. Perhaps there is a method to call from a file - lines by numbers, or any method.

===

since i'm a beginner maybe i don't understand is it possible? look at the picture

Community
  • 1
  • 1
Vision 10
  • 13
  • 5
  • So do you need the shown above meta tags on every page/many pages? – Prometheus Feb 12 '19 at 09:40
  • not quite... I would like to have a one file *.erb. In which there would be such lines: ` ` and next ` – Vision 10 Feb 12 '19 at 09:53
  • From what i understand is, is that you want dynamic meta tags? Or are those static and you have only a limited amount, for example 5? – Prometheus Feb 12 '19 at 09:57
  • No limits, all pages are completely static. Just for ease of editing, I need to call in different layouts files, different parts of one wile *.erb of partials folder. Sorry for my English) – Vision 10 Feb 12 '19 at 10:09
  • As already one posted at the bottom: You can just simply create partials for your meta tags: _metas.html.erb and then render those metas via: <%= render 'shared/metas %> and sotre _metas.html.erb inside a "shared" folder that is located inside your views folder. You could also split those metas up and create partials for these, for example _meta_keywords.html.erb and add only the keyword meta inside of it. Then, render it via: <%= render meta_keywords %> like so. Obviously, you need a different approach if your metas are dynamic but thats not the case like you've said. Cheers! – Prometheus Feb 12 '19 at 10:52
  • see the screenshot please – Vision 10 Feb 17 '19 at 19:07
  • You have to call your files: ".html.erb" not just ".erb" – Prometheus Feb 18 '19 at 08:58
  • in file *.html.erb i call only (layout: ***) no more... – Vision 10 Feb 19 '19 at 12:46
  • Again: go to your views folder -> create a new folder and call it "shared". Inside shared, create a new file called "_meta_keywords.html.erb". Add to this file

    Hello i am meta keyword

    -> go to application.html.erb, inside add: <%= render 'shared/meta_keywords' %> repeat those steps if you need more static partials (like you've said). If you don't follow these steps, it wont work.
    – Prometheus Feb 19 '19 at 12:52
  • @Prometheus Tell me, maybe I'm completely stupid, but if you call as you said (<% = render 'shared / meta_keywords'%>), as you told me. Then all the content will be called - "_meta_keywords.html.erb" right? But I don’t need it, in this file I want to store not only -

    Hello i am meta keyword h3> but also

    Hello i am meta keyword 2 h3> and

    Hello i am meta keyword 3 h3>. And send them not in one file layouts but in several.

    – Vision 10 Feb 20 '19 at 06:34
  • No reason to think this! Problems are solved by trial and error. As for your question: If you have another keyword and you want to seperate them, you just create another file: "_meta_keyword_whatever.html.erb" add whatever you want into that file (as long as it is rails/html syntax) and then render it wherever you want via: <%= render 'shared/meta_keyword_whatever' %>. You need to understand that render partials are just a extension that you can inject everywhere. Lets say you have a image that you want to render in multiple parts of your app. You can add the image to every file, or create a.. – Prometheus Feb 20 '19 at 08:13
  • .. partial inside shared, call it: "_i_dont_care.html.erb", add the image to this file and then again, add it to wherever you want via: <%= render 'shared/i_dont_care.html.erb' %> That is all about partials. Everything else i NOT possible, since its only a extension of an html file. Whatever you want to do inside of it, needs to follow html rules. If you need dynamic metas (which you denied) than you could use: https://github.com/kpumuk/meta-tags that would let you generate dynamic metas based on your db content. We can also chat if you still need help with that. – Prometheus Feb 20 '19 at 08:17

1 Answers1

0

Create a new file for instance "views/_partial.html.erb" ; The filename should begin with an underscore. Put your HTML content in this file which is the following:

<meta name="keywords" content="any keywords" />
<meta name="description" content="any description" />

Now from the view from which you want to call the partial, use the following code:

<%= render :partial => 'views/partial' %>

Note: while calling the partial you should exclude the underscore.

Edit Method 2

File: _partial.html.erb

<% if page == 1 %>
  <meta name="keywords" content="any keywords" />
  <meta name="description" content="any description" />
<% elsif page == 2 %>
  <meta name="keywords" content="any keywords" />
  <meta name="description" content="any description" />
<% end %>

Code to call the partial

<%= render partial: 'partial', locals:  {page: 1} %>
<%= render partial: 'partial', locals:  {page: 2} %>
Amogh Hegde
  • 392
  • 3
  • 10
  • I am interested in the possibility of loading such parts on different pages from one file. – Vision 10 Feb 18 '19 at 17:16
  • No Thats not possible, you will have to create seperate partials for each The other option is to use locals and put conditions on your partial based on the value of the local variable. – Amogh Hegde Feb 19 '19 at 03:43
  • looks very incomprehensible how to try it? don't forget that newbie... thank... also advised (content_for) method for me, but also did not explain how to apply it... If it's not difficult for you, you can describe how to test these methods for my question ? – Vision 10 Feb 19 '19 at 12:45