Using Sapper, How to get the current page's slug in template.html - svelte-3

Going through the docs, it seems like Sapper's template.html only has these tags available:
%sapper.base%
%sapper.styles%
%sapper.head%
%sapper.html%
%sapper.scripts
What I'd love to have is the current page's slug to use it like so:
<!-- bottom of template.html -->
<img src="https://piratepx.com/page={currentPageSlug}"
alt="" style="display:none;"/>
</body>
</html>
It doesn't need to be available to JS, just added in the HTML.
I'm exporting to static and this would be a trivial task with most Static Site Generators but I can't find the obvious solution with Sapper.

Considering you are using Sapper I would do this in _layout.svelte instead:
<script>
import { stores } from '#sapper/app';
const { page } = stores();
$: currentPageslug = $page.params.slug
</script>
<img src="https://piratepx.com/page={currentPageSlug}" alt=""/>
With this technique you can also add a default value, or some condition in case there is no slug.

Related

How to add option in <redoc>?

I want to add some additional option to my ReDoc. For current implementation I am using json file that is generated from Swagger, and this is added in html page. Example how this is done:
<body>
<redoc spec-url='http://petstore.swagger.io/v2/swagger.json'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc#next/bundles/redoc.standalone.js"> </script>
</body>
I use this as referent documentation: https://github.com/Rebilly/ReDoc
How can I add option object in tag and not use ReDoc object? And how can I use vendor extension e.g. x-logo?
In documentation this is set via json file, but my json file is auto generate from Swagger.
You just place the options after the spec-url in the redoc tag like this:
<body>
<redoc spec-url='http://petstore.swagger.io/v2/swagger.json' YOUR_OPTIONS_HERE></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc#next/bundles/redoc.standalone.js"> </script>
</body>
in this example on ReDoc repository you can verify it (line 22 at this moment):
https://github.com/Rebilly/ReDoc/blob/master/config/docker/index.tpl.html#L22
Important:
Remember to "kebab-casing the ReDoc options", as an example if your options are:
hideDownloadButton noAutoAuth disableSearch
YOUR_OPTIONS_HERE
should be (after kebab-casing them):
hide-download-button no-auto-auth disable-search
Your body with those options becomes like this:
<body>
<redoc spec-url='http://petstore.swagger.io/v2/swagger.json' hide-download-button no-auto-auth disable-search></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc#next/bundles/redoc.standalone.js"> </script>
</body>
Hope it will be usefull to you.
ReDoc has advanced initialization via Redoc.init so you can download the spec manually and add some postprocessing (e.g. add an x-logo).
You can pass ReDoc options as the second argument to Redoc.init:
<body>
<div id="redoc"></div>
<script src="https://cdn.jsdelivr.net/npm/redoc#next/bundles/redoc.standalone.js"> </script>
<script>
fetch('http://petstore.swagger.io/v2/swagger.json')
.then(res => res.json())
.then(spec => {
spec.info['x-logo'] = { url: "link/to/image.png" };
Redoc.init(spec, {
// options go here (e.g. pathInMiddlePanel)
}, document.getElementById('redoc'));
});
</body>
NOTE: This requires Fetch API to be available in browsers so it won't work in IE11.
You can place your options next to spec-url.
Be sure that the version of Redoc you are using, have options you want to use, you can check it by going to the specific version. github.com/Redocly/redoc/tree/vx.x.x.
As a side note features lazy-rendering in available till v1.22.3.
https://github.com/Redocly/redoc#redoc-options-object
You can use all of the following options with standalone version on tag by kebab-casing them, e.g. scrollYOffset becomes scroll-y-offset and expandResponses becomes expand-responses.

Umbraco 4.9 Display Media Picker Image in Template

I'm trying to allow content editors to be able to choose the main banner image on a page by having it chosen through a Media Picker property.
I've tried the standard inline XSLT of:
<umbraco:Item runat="server" field="banner" xslt="concat('<img src="', umbraco.library:GetMedia({0},0)/umbracoFile, '" />')" xsltDisableEscaping="true" />
But in my simple template of:
<asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
<header class="home-header">
<div class="logo-wrapper">
<umbraco:Item runat="server" field="banner" xslt="concat('<img src="', umbraco.library:GetMedia({0},0)/umbracoFile, '" />')" xsltDisableEscaping="true" />
</div>
</header>
</asp:Content>
The rendered HTML comes out at:
<header class="home-header">
<div class="logo-wrapper">
</div>
</header>
I've read about using a macro to render images but my Umbraco knowledge is limited. If someone could provide steps for actually adding an XSLT macro, I'd be happy to try that out.
Unfortunately we are stuck on Umbraco v4.9 for now too so no <umbraco:Image /> tag for me.
I suggest you use a c# Umbraco macro instead of xslt. Umbraco 4.9 can do that.
An macro can in a differt file or simple use a inline macro:
<umbraco:Macro runat="server" language="cshtml">
#if (#Model.visual != "")
{
<img src="#Model.Media("banner", "umbracoFile")" class="foto" />
}
</umbraco:Macro>
Same as <img src="#node.Media("banner", "umbracoFile")" />
If anyone else finds this and you are not using MVC you can use this approach inside your template to get the image path you selected from the media picker
<umbraco:Item field='headerImage' runat='server'xslt='umbraco.library:GetMedia({0},true())/umbracoFile'xsltDisableEscaping='true'></umbraco:Item>
Where headerImage is the alias for your attribute name in your document type. This will render something like "/media/1002/sample.jpg" for instance

Best way to show image previews before upload in rails & carrierwave

I've been using rails for past few days and wanted to know what is best way to show image previews before upload in rails & carrierwave.
I came across a few options like using plupload or jquery file upload or using uploadify.
If you only need image preview in a form before upload, you (as me) will see that JQuery Upload plugin is just too much complex and not so easy to run properly (I was able to see the preview, but then I couldn't upload the picture).
http://saravani.wordpress.com/2012/03/14/preview-of-an-image-before-it-is-uploaded/
It's simple and fast to code.
I put the code here just in case the source dies:
Script:
<!-- Assume jQuery is loaded -->
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#img_prev')
.attr('src', e.target.result)
.width(150)
.height(200);
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
In the HTML:
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<input type='file' onchange="readURL(this);" />
<img id="img_prev" src="#" alt="your image" />
</body>
You can use the plugin jQuery File Upload which it's a full solution, but if you need something less robust, you can also try using FileReader directly, like this, so you can customize the functionality to whatever you need.
According to the documents image_cache is showing what you are uploading.
<%= f.hidden_field :image_cache %>

Is it possible to add dynamic jQuery tabs using KnockoutJS Templates?

I'm trying to build a dynamic display in a web page. The site is built in ASP.NET MVC 3, but I'm not sure if this matters...
Essentially, I have a base "_Editor.cshtml" page which uses partials to render the specific editor content in my display using query parameters. Consider an editor for a chart (line chart vs. bar chart).
_Editor.cshtml
<div id='tabs'>
<ul>
<li><a href='#queryTab'>Define Query</a></li>
<!-- THIS IS WHERE I NEED TO SHOW ALL OF MY 'CUSTOM' TAB NAMES -->
<!-- ko template: 'tabNames' -->
<!-- /ko -->
<li><a href='#themeTab'>Styles and Themes</a></li>
</ul>
<div id='queryTab'>
<!-- configure db connection, tables, fields, etc... (not important) -->
</div>
<!-- THIS IS WHERE I WANT TO SHOW CONTENT FOR MY CUSTOM TABS -->
<!-- ko template: 'tabContent' -->
<!-- /ko -->
<div id='themeTab'>
<!-- show various style options here (not important) -->
</div>
</div>
<script type="text/javascript">
$(function(){
var vm = { /* define my model here */ };
ko.applyBindings(vm);
$("#tabs").tabs();
});
</script>
#if (Model.EditorType == ChartTypes.Bar) {
#Html.Partial("_BarChartEditor")
} else if (Model.EditorType == ChartTypes.Line) {
#Html.Partial("_LineChartEditor")
}
_BarChartEditor.cshtml
<script id="tabNames" type="text/html">
<li>Bar Chart Tab!</li>
<!-- I could have many more tab names here -->
</script>
<script id="tabContent" type="text/html">
<div id="barChartTab">
<h1>Add controls for bar chart configuration</h1>
</div>
<!-- I would create a div for each tab name above here -->
</script>
_LineChartEditor.cshtml
<script id="tabNames" type="text/html">
<li>Line Chart Tab!</li>
</script>
<script id="tabContent" type="text/html">
<div id="lineChartTab">
<h1>Add controls for line chart configurations</h1>
</div>
</script>
Sorry for the lengthy code drop (it took a long time to write it all here, so have mercy on me). :) I wanted to make sure that my problem was understood because of the way I'm building my editors using custom partials. Perhaps it's cludgy, but it works for the most part...
Really, everything works great except for the tab content. The tab rendering appears to be happening before I'm able to bind my view model to the UI. When the 'ko.applyBindings()' method is called, the tab shows up on different tabs.
Has anybody tried to do this? Has anybody had success? I've created a jsfiddle to show a simple scenario to show exactly what I'm talking about:
http://jsfiddle.net/TrailHacker/j2nhm/
Thanks for any help!
I actually got it working with your example, your content template was just structured incorrectly. It was missing the <div> tags.
If you modify this for your example, just remember that the div id needs to match the link's ref. You can throw both of these values into your viewmodel, to allow for multiple custom tabs.
http://jsfiddle.net/tyrsius/UCGRZ/

change the image of a link for active , hover(mouseover) and mouseleave in MVC masterpage

In my requirement i want to change the background image of a link for hover,mouse out and active in my master page i tried a couple ways but it is not giving a right solution for me .Kindly any one guide me to get the solution and my format is like the below
<a href='<%: Url.Action("ListTask", "Task") %>'>
<img id="taskImage" src='<%: Url.Content("~/Content/Images/MasterPage/TaskMenuNormal.png") %>' onmouseover="this.src='../Content/Images/MasterPage/TaskMenuHover.png'"
onmouseout="this.src='../Content/Images/MasterPage/TaskMenuNormal.png'" /></a>
the above code will work fine and here i need to fix the hover image at the active of the link and not to be changed at the time of active view(MVC) in the master page.(This code will be in the master page).
can anyone provide solution in javascrip or jqery.
Thanks.
you could do:
$('#taskImage').hover(function(){
$(this).attr('src','../Content/Images/MasterPage/TaskMenuHover.png');
}
$('#taskImage').mouseout(function(){
$(this).attr('src','../Content/Images/MasterPage/TaskMenuNormal.png');
}
Use the hover function. The first parameter is used on mouseover, the second on mouseout.
HTML
<img id="taskImage" src="<%: Url.Content("~/Content/Images/MasterPage/TaskMenuNormal.png") %>" />
jQuery
$('#taskImage').hover(
function() {
$(this).attr('src','../Content/Images/MasterPage/TaskMenuHover.png');
},
function() {
$(this).attr('src','../Content/Images/MasterPage/TaskMenuNormal.png');
}
);
Personally, I'd amend your markup to include the image in the A tag as a background in CSS. then you can use A:hover, and A:active CSS states to amend your link. This then means no javascript needs to be used.

Resources